MATLAB: How to remove NaN values from a 7862×6 cell

cell arraysfor loopnan

Hello, Matlab community:
I have a 7862×6 cell (variable name = A) that has NaN values at the end of some of the columns of data (hundreds of consecutive data points). How can I remove the NaNs so that I can find the true length of each column? I have looked through many current threads, but those solutions have not worked for my purpose. I think the problem is that I need a loop to work through the columns and rows, though I am not sure how to approach it. Any suggestions are appreciated!
– John

Best Answer

Presuming can decipher the actual storage you're trying to describe, here's a sample and solution to the number...
>> A=nan(10,3); for i=1:3,A(1:randi(8,1),i)=i;end % create some dummy data
>> A=num2cell(A)
A =
[ 1] [ 2] [ 3]
[ 1] [ 2] [ 3]
[ 1] [ 2] [ 3]
[ 1] [ 2] [ 3]
[ 1] [ 2] [ 3]
[ 1] [NaN] [ 3]
[ 1] [NaN] [NaN]
[ 1] [NaN] [NaN]
[NaN] [NaN] [NaN]
[NaN] [NaN] [NaN]
>> sum(isfinite(cell2mat(A))) % number non-NaN each column
ans =
8 5 6
>>
Since A is a cell array, you can remove those rows on a column-by-column basis if desired but isn't necessary to determine the sizes and may be more convenient if don't 'cuz can convert as-is to an 'ordinary' array which can make referencing and calculations simpler, depending on what else is needed.