Reputation: 79
I have a 2D cell array where the first row consists of labels from a database, such that:
myData(1,:) = {'X', 'Y', 'Z', 'W'};
For each label, there is a column (i.e. from the second row to the last row for the particular column) that consists of cell entries, but there may be situations where not all of the cells are populated.
This example should illustrate an example of what happens with what I'm working with:
myData =
'X' 'Y' 'Z' 'W'
[1] [] [] [1]
[2] [] [] [2]
[] [] [] [3]
Would it be possible to determine how many non-empty rows there are for each column of my 2D cell array, not including the first row of labels?
Upvotes: 1
Views: 61
Reputation: 112659
Is this what you want?
sum(~cellfun('isempty', myData))-1
The ~cellfun(...)
part gives 1
for each cell of your 2D cell array that is not empty, and 0
otherwise. Then the sum for each column is computed (sum(...)
); and 1
is subtracted so that the first row is not considered. The result in your example is:
>> myData
myData =
'X' 'Y' 'Z' 'W'
[1] [] [] [1]
[2] [] [] [2]
[] [] [] [3]
>> sum(~cellfun('isempty', myData))-1
ans =
2 0 0 3
Upvotes: 3