How do I count the number of rows in a cell array's cells?

55 次查看(过去 30 天)
My goal is to create a table as a subset of a cell array's contents. I understand that it's better to preallocate memory size and then truncate empty rows rather than have an array grow with each loop repetition. So I wish to count the total number of rows in each cell as the maximum possible size of the resulting table.
This does not work:
>> size(newvoldata)
ans =
1 200
Nor does this:
>> numel(newvoldata)
ans =
200
For example, one cell can have a table such that
>> height(newvoldata{160})
ans =
439
Is there a MATLAB command that more quickly does the following?
counter = 0;
for loop = 1:length(newvoldata)
counter = counter + size(newvoldata{loop},1);
end
counter
I suspect there is some command similar to numel that does this for cell arrays. Is there one? Or should I use an implicit function ?
  1 个评论
Greg
Greg 2018-1-17
编辑:Greg 2018-1-17
Is your end goal just to know the height of each cell's table? Or are you eventually going to do some form of join on the tables?

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2018-1-17
编辑:Stephen23 2018-1-17
For a cell array of tables:
sum(cellfun(@height,newvoldata))
For a cell array of numeric/char/cell/struct arrays:
sum(cellfun('size',newvoldata,1))
  12 个评论
Stephen23
Stephen23 2018-2-15
编辑:Stephen23 2018-2-15
"Just tried both commands and they both failed"
Which "both commands" are you referring to?
Did you use a logical mask like I showed you in a comment nearly one month ago? If you have cells containing a 0x0 numeric then you will need to use a mask, which is why I showed you how to generate and use one.
"The second results in '199', apparently counting the number of non-empty cells"
The second solution I gave in my answer should not count non-empty cells: for a vector cell array it sums the first dimension of all contents of that cell array. To count the non-empty cells you would need something like this:
sum(~cellfun('isempty',newvoldata))
which, depending on the size of the contents of the cell array, may or may not give the same output value. It is possible that the cellfun "backwards compatibility" syntax using 'size' has not been updated to work correctly with tables.
As has been requested before: if you want help then please upload your data in a .mat file.
Daniel Bridges
Daniel Bridges 2018-2-15
编辑:Daniel Bridges 2018-2-15
Thank you for this reminder. I did not apply a logical mask as you advised. I tried the two commands you posted in the answer here, sum(cellfun(@height,newvoldata)) and sum(cellfun('size',newvoldata,1)).
You're saying the second one shouldn't be counting non-empty cell [contents?] but it's the only guess I have for why the result is '199', whereas that cell array had 199 tables and 1 0x0 double. The first cell's table did not have height 199 (in fact, I think none of them did).
For the sake of accepting this answer, I just tested to verify that the first command works:
test = table(5,'VariableNames',{'john'});
test2 = table([5;3],'VariableNames',{'billy'});
test3 = table({'jim';'sally';'roger'},[1;5;3],'VariableNames',{'the' 'teh'});
cellarray = cell(1,3);
cellarray{1} = test; cellarray{2} = test2; cellarray{3} = test3;
ans = sum(cellfun(@height,cellarray))

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by