MATLAB Answers

How to continue while loop if NaN value is present in cell array

7 views (last 30 days)
Luke McCauley
Luke McCauley on 15 Jun 2020
Commented: Luke McCauley on 17 Jun 2020
I am using a cell 1x54 cell array containing 600x8 double values. In this array, i am looking at one column in particular for each array cell (column 5) and i want to count the number of seconds it takes until the data crossess a threshold value.
I am able to simply do this with a while loop except my data has NaN values which are causing the while loop to stop. I want to count these NaN values and keep the loop progressing until that threshold is reached.
I have a row vector 'Timetosteady' which i want to count the steps into (eventually making it a 1x54 vector) and already have calculated a threshold "MinC" which is a 1x54 row vector containing threshold values.
I am encountering two issues. Firstly using this loop trying to calculate across all cells in the cell array:
TimetosteadyC=NaN(1,54);
i=1;
for j=1:54
while ((C{1,j}(i,5))< MinC(i) || isnan(C{1,j}(i,5)))
TimetosteadyC(j)=i+1;
i=i+1;
end
end
i receive the error "Index exceeds the number of array elements (54).". There defintely are 54 columns in my array (for example if i use C{1,54}(1,5) i receive a value) so why is this occuring?
Secondly, if i use this code only assessing in the first cell array:
TimetosteadyC=NaN(1,54);
i=1;
for j=1
while ((C{1,j}(i,5))< MinC(i) || isnan(C{1,j}(i,5)))
TimetosteadyC(j)=i+1;
i=i+1;
end
The code does skip the first NaN value seen in the column i am looking at within the array but then stops before the threshold has been reached (the 'Timetosteady' value spits out as 25 when it should be closer to 150)
I would really appreciate guidance on how to best create a loop in a cell array that sequentially counts the values in a specific column until the threshold has been crossed while including NaN values.
btw i am on R2019b
Thanks

  0 Comments

Sign in to comment.

Accepted Answer

Matt J
Matt J on 15 Jun 2020
Edited: Matt J on 16 Jun 2020
Since all your arrays C{j} are the same size, it seems unnecessary to carry the data around in a cell array at all. You could just concatenate them into a 3D array:
c=cat(3,C{:});
Now, you can generate the result using simple numeric array indexing. Also, no loops are required:
c5=c(:,5,:);
[~,Timetosteady]=min( c5<reshape(MinC,1,1,[]) | isnan(c5) );
Timetosteady=Timetosteady(:);

  3 Comments

Luke McCauley
Luke McCauley on 16 Jun 2020
Hi Matt,
Using a 3d array is a great idea, thank you for your help. I am not very familiar with using this in Matlab yet so i may need a bit more help.
When i run the code c and c5 correctly run but i have the following issue appear:
Array dimensions must match for binary array op.
Error in SteadyStateCount (line 45)
[~,Timetosteady]=min( c5<MinC(:) | isnan(c5) )
Is this because my MinC is still a 2D array do you think? Do you have suggestons on how to fix this?
Thanks
Matt J
Matt J on 16 Jun 2020
Try it now with my latest edit.
Incidentally, in your original code, you have MinC(i) when it should really be MinC(j).

Sign in to comment.

More Answers (0)


Translated by