MATLAB Answers


How to use polyfit for a table of values

Asked by Daniel Ibarra on 7 Oct 2019
Latest activity Commented on by Matt J
on 8 Oct 2019
I am trying to fit a curve to data i read in from a excel file. This is the code i have so far. I've been trying to debug it for some time and the line that is giving me problems is when i use polyfit. The values that it gives for constants are NAN and NAN.
cardatatable = xlsread('carmpg.csv')
mpg = cardatatable(:,1);
horsepower = cardatatable(:,4);
xlabel('miles per gallon')
p = polyfit(mpg,horsepower,1);
mpgfit = linspace(min(mpg),max(mpg),1000);
horsepowerfit = polyval(P,mpgfit);
hold on


Sign in to comment.


1 Answer

Answer by Matt J
on 7 Oct 2019
Edited by Matt J
on 7 Oct 2019
 Accepted Answer

That would occur if mpg or horsepower contain NaNs. You should remove them.


Thank you, this fixed the issue.
Matt J
on 8 Oct 2019
You're welcome, but please Accept-click the answer if it resolved your question.

Sign in to comment.