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

# 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..

## Products

No products are associated with this question.

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

Hello kity on 21 Dec 2012

sum(your_count)

thank you

José-Luis on 21 Dec 2012

My pleasure

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.