I used most of ideas but it did not work then with the help of what herohuyongtao said i reach to this idea which worked properly
[nr,nc]=size(x)
Which nr is the number of rows thanks all of you.
Answer from SaraDean on Stack OverflowI used most of ideas but it did not work then with the help of what herohuyongtao said i reach to this idea which worked properly
[nr,nc]=size(x)
Which nr is the number of rows thanks all of you.
A slightly more general approach: works for rows or columns, and takes into account the size of each cell:
dim = 1; %// 1 for rows, 2 for columns
result = sum(cellfun(@(c) size(c,dim), a), dim);
Example:
>> a = {1, [2 3], []; 4, [], 5}
a =
[1] [1x2 double] []
[4] [] [5]
>> dim = 1;
gives
>> result = sum(cellfun(@(c) size(c,dim), a), dim)
result =
2 1 1
This is identical to your last (now deleted) question, just use size instead of length:
D = cellfun(@(x)(size(x,1)), C)
But note that cellfun is just a wrapper for a for-loop so doing this does not avoid loops.
Note a better solution (from Luis Mendo's comment) is
[D, ~] = cellfun(@size, C)
This way you can get the number of rows and the number of columns in one shot:
[nr, nc] = cellfun(@size, c)
Can you please try this two instructions :
cellsz = cellfun(@size,C,'uni',false);
cellsz{:}
you will get something like :
ans =
4 2
ans =
3 1
ans =
5 3
You need to access each member of the cell array separately, you are looking for the size of the data contained in the cell - the cell is the container. Two methods
for loop:
cell_content_lengths=zeros(1,length(a));
for v=1:length(a)
cell_content_lengths(v)=length(a{v});
end
cellfun:
cell_content_lengths=cellfun(@length,a);
Any empty cells will just have length 0. To extend the for-loop to matrices is trivial, and you can extend the cellfun part to cells containing matrix by using something like this, if you are interested:
cell_content_sizes=cell2mat(cellfun(@length,a,'uniformoutput',false));
(Note for the above, each element of a needs to have the same dimension, otherwise it will give errors about concatenating different size matrices)
EDIT
Based on your comment I think I understand what you are looking for:
non_empty_cols = sum(~cellfun(@isempty,a),2);
With thanks to @MZimmerman6 who understood it before me.
So what you're really asking, is "How many non-empty elements are in each row of my cell array?"
filledCells = ~cellfun(@isempty,a);
columns = sum(filledCells,2);
Since you are only interested in the number of elements, here is a simplified version of flatten.m that @Ansari linked to:
function n = my_numel(A)
n = 0;
for i=1:numel(A)
if iscell(A{i})
n = n + my_numel(A{i});
else
n = n + numel(A{i});
end
end
end
The result:
>> C = {{{1,2},{3,4,5}},{{{6},{7},{8}},{9}},10};
>> my_numel(C)
ans =
10
EDIT:
If you are feeling lazy, we can let CELLPLOT do the counting:
hFig = figure('Visible','off');
num = numel( findobj(cellplot(C),'type','text') );
close(hFig)
Basically we create an invisible figure, plot the cell array, count how many "text" objects were created, then delete the invisible figure.
This is how the plot looks like underneath:

Put this in a function (say flatten.m) (code from MATLAB Central):
function C = flatten(A)
C = {};
for i=1:numel(A)
if(~iscell(A{i}))
C = [C,A{i}];
else
Ctemp = flatten(A{i});
C = [C,Ctemp{:}];
end
end
Then do numel(flatten(C)) to find the total number of elements.
If you don't like making a separate function, you can employ this clever (but nasty) piece of code for defining a flatten function using anonymous functions (code from here):
flatten = @(nested) feval(Y(@(flat) @(v) foldr(@(x, y) ifthenelse(iscell(x) | iscell(y), @() [flat(x), flat(y)], @() {x, y}), [], v)), nested);
Either way, you need to recurse to flatten the cell array then count.