mHelpMe
mHelpMe

Reputation: 6668

subindex into a cell array of strings

I have a 6 x 3 cell (called strat) where the first two columns contain text, the last column has either 1 or 2.

I want to take a subset of this cell array. Basically select only the rows where the last column has a 1 in it.

I tried the following,

ff = strat(strat(:, 3), 1:2) == 1;

The error message is,

Function 'subsindex' is not defined for values of class 'cell'.

How can I index into a cell array?

Upvotes: 0

Views: 626

Answers (1)

chipaudette
chipaudette

Reputation: 1675

Cell arrays are accessed through braces {} instead of parentheses (). Then, as a 2nd subtlety, when pulling values out of a cell arrays, you need to gather them...for numerics you gather them into regular arrays using [] and for strings you gather them into a new cell array using {}. Confusing, eh?

ff = { strat{ [strat{:,3}]==1 , 1:2 } };

Gathering into cell arrays this way can often give the wrong shape when you're done. So, you might try something like this

ind = find([strat{:,3}]==1);  %find the relevant indices
ff = {{strat{ind,1}; strat{ind,2}}';  %this will probably give you the right shape

Upvotes: 1

Related Questions