Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

How to count the amount of cells?

Asked by Hello kity on 21 Dec 2012

Hi

I have a cell array, 1x 90. eacht of them has diff set of data,

for example, 1x1 has 10 cells , 1x2 5 cell etcs.

i want to count to count the amount of cells

numel(cellarrayname) gives 90... thats not what i want.

it should count every cell array add cellarray 2 +cell aray 3 etc..

0 Comments

Hello kity

Products

No products are associated with this question.

2 Answers

Answer by José-Luis on 21 Dec 2012
Accepted answer
 your_count = cellfun(@(x) numel(x),your_cell_array);

2 Comments

Hello kity on 21 Dec 2012

adding,

sum(your_count)

thank you

José-Luis on 21 Dec 2012

My pleasure

José-Luis
Answer by Wayne King on 21 Dec 2012
Edited by Wayne King on 21 Dec 2012

You want to count how many cells are in each element of the cell array?

     x = cell(3,1);
     x{1} = cell(2,1); x{2} = cell(25,1); x{3} = cell(30,1);  
     out = cellfun(@numel,x,'uni',0);
     numberofelements = sum(cell2mat(out));

In the above, cellfun() gives you cell array with each cell containing the number of elements in each cell of your original cell array.

cell2mat() turns that cell array into a vector, so now you have a vector where each elements gives the number of each elements in the cell arrays of your original cell array.

Finally, I just sum the elements of the vector to get the total number of elements.

0 Comments

Wayne King

Contact us