I am met with the error "Undefined function or variable 'isnan'" when running this code:
%counter = 1:4
%a is a 220x4 cell
%data is a 220x4 double
for i = size(a, 1)
pairs = [];
pairs(:,1) = counter;
pairs(:,2) = data(i,:);
pairs = pairs(~isnan(pairs(:,2),:));
slope(i) = polyfit(pairs(:,1),pairs(:,2),1);
end
I can't seem to figure out exactly why. Is the way I'm using the NOT operator?

3 commentaires

What is the result of:
which isnan -all
Carver Nabb
Carver Nabb le 7 Jan 2019
Screen Shot 2019-01-07 at 2.54.36 PM.png
nanren888
nanren888 le 7 Jan 2019
As Walter says; It is complaining about the two-parameter isnan(x,y) that you've invented :)

Connectez-vous pour commenter.

 Réponse acceptée

Walter Roberson
Walter Roberson le 7 Jan 2019
You have
pairs = pairs(~isnan(pairs(:,2),:));
You want
pairs = pairs(~isnan(pairs(:,2)),:);

21 commentaires

nanren888
nanren888 le 7 Jan 2019
Yes, it is complaining about the two-parameter isnan(x,y) that you've invented :)
How difficult could it be to produce a better error message?
>> isnan( 17, : )
Undefined function or variable 'isnan'.
Carver Nabb
Carver Nabb le 7 Jan 2019
Thank you Walter!
I do have a question about an error with this same code, but about the line:
slope(i) = polyfit(pairs(:,1),pairs(:,2),1);
Would it be best to create a new question? It is telling me the indices on the left side are not compatible with the right.
It is an oddity of the parser, which has some messy behaviour for colon.
functionname(argument, :)
will disable recognition of functionname as a function and will look for a variable with that name, probably resulting in the misleading error message.
A = reshape(1:60, 2, []);
A(1:2)
%1:2 gets computed as [1 2] first, then the datatype of A is looked up
%and since it is a variable, subsref is invoked to select the first two elements
A('1:2')
%'1:2' is a vector of characters, equivalent to char([49 58 50]) .
%The numeric equivalent is taken, [49 58 50] and are used as subscripts into A
A(:)
%the : is transformed into subsref() with type '()' and index {':'}
%which is treated by subsref as a reshape request
A(':')
%the ':' with the character scalar ':' is transformed into subsref() with
% type '()' and index {':'} which is treated as a reshape request
A(1, :)
%this is transformed into subsref() with type '()' and index {1, ':'}
%which is treated by subsref as a subset indexing request
A(1, ':')
%this is transformed into subsref() with type '()' and index {1, ':'}
%which is treated by subsref as a subset indexing request
A(1, '1:2')
%this is transformed into subsref() with type '()' and index {1, '1:2'}
%which is treated by subsref as a subset indexing request with
%the '1:2' being treated as equivalent to [49 58 50]
but
functionname(argument, ':')
is not treated as a subsref request.
Walter Roberson
Walter Roberson le 7 Jan 2019
polyfit() with third parameter N requests a fitting in which the maximum exponent used for the independent variable is N. Since exponents all the way down to 0 are used, that results in N+1 results. So when you requested polyfit(x,y,1) you are going to get out 1+1 = two coefficients, one for x^1 and one for x^0 (the constant term)
Carver Nabb
Carver Nabb le 7 Jan 2019
So, just so I'm clear, I need to change the exponent to 0, as well as place the colons in ' ' ? I apologize if this is a silly question, I am quite new to programming.
Carver Nabb
Carver Nabb le 7 Jan 2019
When I run the script, after changing the exponent to 0, it runs without error however all values for slope are 0 as well.
For the isnan() matter, you just accidentally had the : inside the isnan function call and needed to move it outside that call into a place it was acting as a subscript.
polyfit(pairs(:,1),pairs(:,2),0)
is always exactly equal to mean(pairs(:,2)) even when the x are irregularly spaced. If you want only a single coefficient then there is no point using polyfit().
If you want to fit the data to a straight line then you would need to expect two coefficients as output, like
mc = polyfit(pairs(:,1),pairs(:,2),1);
slope(i) = mc(1);
@Carver Nabb: Use:
pairs = pairs(~isnan(pairs(:,2)),:);
Again the debugger will help you to understand, what happens. dbstop if error, run the code again, and when it stops:
polyfit(pairs(:,1),pairs(:,2),1)
size(polyfit(pairs(:,1),pairs(:,2),1))
size(slope(i))
You cannot assign a vector to a scalar. I guess you want:
P = polyfit(pairs(:,1),pairs(:,2),1);
slope(i) = P(2);
I am still getting Zeros for all of the slope outputs. Here is my revised code. It runs without prompting any errors. Thank you again for the help, I greatly appreciate it.
for i = size(a, 1)
pairs = [];
pairs(:,1) = counter;
pairs(:,2) = data(i,:);
pairs = pairs(~isnan(pairs(:,2)),:);
P = polyfit(pairs(:,1),pairs(:,2),1);
slope(i) = P(1); %I also tried with P(2)
end
Walter Roberson
Walter Roberson le 8 Jan 2019
Question: why is your for loop to size(a,1) but you are not accessing a in the loop? It would look to me to make more sense to run to size(data,1)
Carver Nabb
Carver Nabb le 8 Jan 2019
Agreed. I've already made the change; a was a cell that i changed to a matrix (data) using cell2mat. They're the same size so it didn't make much of a difference.
Walter Roberson
Walter Roberson le 8 Jan 2019
Are you happening to see warnings such as
Warning: Polynomial is not unique; degree >= number of data points.
?
Carver Nabb
Carver Nabb le 8 Jan 2019
No, the only tip I see is "The variable 'slope' appears to change size on every loop iteration. Consider preallocating for speed.
Walter Roberson
Walter Roberson le 8 Jan 2019
Can you attach a .mat with counter and data ?
Carver Nabb
Carver Nabb le 8 Jan 2019
Here you are. Thanks for the help, Walter.
Carver Nabb
Carver Nabb le 8 Jan 2019
It looks like the allpcompst.mat I've sent you already has the NaN values. So a part of the code is unnecessary.
for i = size(allpcompst,1)
should be
for i = 1:size(allpcompst,1)
You will get messages Warning: Polynomial is not unique; degree >= number of data points. 207 of the 220 slopes are non-zero.
Carver Nabb
Carver Nabb le 8 Jan 2019
Thank you, this does produce non-zero values. However, they are not the correct slopes, for example, I know the first slope ought to be 3.6, and the last, -4.6. However the first value returned with this code is 113.
Carver Nabb
Carver Nabb le 8 Jan 2019
I figured it out! Walter and Jan, thank you so much for the help. I do appreciate it.

Connectez-vous pour commenter.

Plus de réponses (2)

Jan
Jan le 7 Jan 2019
Although I assume that Walter hit the point already, I suggest to use teh debugger to identify such problems. Type in the command window:
dbstop if error
Now run the code again until it stops. Check the currently processed line by evaluating it piecewise:
% pairs = pairs(~isnan(pairs(:,2),:))
pairs(:,2)
~isnan(pairs(:,2),:) % <- This should fail already
pairs(~isnan(pairs(:,2),:))
As Walter has said already, isnan(x, :) is meaningful only, if it is an array. Otehrwise using the colon as second input is not valid, if the function isnan() is meant. The error message "Undefined function or variable 'isnan'" is not really clear here. But splitting the commands into parts using the debugger helps to understand the problem.
Kholoud Alzoubi
Kholoud Alzoubi le 19 Avr 2021

0 votes

Undefined function or variable 'textdata'.
Error in KopackRFNNGOA (line 15)
x1=textdata(~isnan(textdata)); %temperature

1 commentaire

Walter Roberson
Walter Roberson le 19 Avr 2021
Not enough context.
You are trying to use a variable named textdata that has not been assigned to.
Also, the variable name would tend to suggest that the variable is not expected to hold numeric data, so isnan() is questionable here.

Connectez-vous pour commenter.

Catégories

Produits

Version

R2018a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by