MATLAB: How to find the length of a cell array in a particular dimension excluding 0x0 doubles

cell arrayscellfunempty cellisemptyMATLAB

As a simple example, say I had this cell array:
examplecell =
{'Name1'} {'Heights'} {'Weights'} {[20000]} {[365]} {[7]}
{'Name2'} {'Heights'} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
{'Name3'} {0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
In this 2-D array, I would want to find the number of elements in a specific row which are not {0x0 double}. It is important that the output is for the particular row, not the cell array overall e.g. for the first row the result would be 6, but for the second row it would be 2, and for the third it would be 1. In practice, the cell array is 6-D.

Best Answer

You can use ~cellfun('isempty',examplecell) to find out if cells are non-empty, and then you can use sum (specifying a dimension) to count the number of non-empty cells in a particular dimension.
Edit:
This method is much faster than cellfun(@isempty,___), but it doesn't work for classes where isempty is overloaded. At least the string, table, and datetime classes are such examples, but there may be more.
examplecell = [{'Name1'} {'Heights'} {'Weights'} {[20000]} {[365]} {[7]};{'Name2'} {'Heights'} {[]} {[]} {[]} {[]};{'Name3'} {[]} {[]} {[]} {[]} {[]}]
examplecell = 3x6 cell array
{'Name1'} {'Heights' } {'Weights' } {[ 20000]} {[ 365]} {[ 7]} {'Name2'} {'Heights' } {0×0 double} {0×0 double} {0×0 double} {0×0 double} {'Name3'} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
fprintf('char syntax takes %.2f microseconds\n',1000*1000*timeit(@() ~cellfun('isempty',examplecell)))
char syntax takes 6.14 microseconds
fprintf('handle syntax takes %.2f microseconds\n',1000*1000*timeit(@() ~cellfun(@isempty,examplecell)))
handle syntax takes 16.01 microseconds
(on my machine the difference is even larger: 2 vs 9)