Selecting and Manipulating Specific Elements from Cell Array

1 view (last 30 days)
A shortened version of my cell array:
{apples} {[1]}
{bananas} {[3]}
{apples} {[10]}
{apples} {[9]}
{bananas} {[7]}
I would like to write a code that creates a cell array that only consists of values from the second column that are on the same row as apples, i.e., this:
{[1]}
{[10]}
{[9]}
How would I do this?
Thanks,
Jonathan

Accepted Answer

Jon
Jon on 27 Jul 2020
Edited: Jon on 27 Jul 2020
% define original matrix
myCell = {'apples' 1;'bananas',3;'apples' 10;'apples',9;'bananas' 7}
% use logical indexing to just get rows that match your criteria
newCell = myCell(strcmp(myCell(:,1),'apples'),2)

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!