How do i omit NaN values from polyfit?
46 views (last 30 days)
Show older comments
I am pulling data from an excel sheet that has empty cells in it which appear as NaNs. I am plotting the data in this excel file, and i want a best fit line to go through it. If i try to use the polyfit function, it returns NaN values because of the empty cells in the excel sheet. Is there anyway i can have the sheet omit the NaN values and only look at the cells with data in them?
filename = 'NB-Aroostook.xls';
numData = xlsread(filename);
[rows, columns] = size(numData);
x=(1:rows);
length= x.';
maxt=numData(1:rows,3);
figure(1)
hold on;
plot(length, maxt, 'LineWidth', .2);
pbaspect([3 1 1]);
fit1=polyfit(length,maxt,1);
plot(length,fit1,'Color','k');
xlabel('Year');
ylabel('Maximum Monthly Temperature (°C)');
Any help is appreciated!
0 Comments
Accepted Answer
Akira Agata
on 16 Mar 2018
How about the following?
filename = 'NB-Aroostook.xls';
numData = xlsread(filename);
maxt = numData(:,3);
length = (1:size(numData,1))';
idx = any(isnan(maxt));
fit1 = polyfit(length(~idx),maxt(~idx),1);
2 Comments
Akira Agata
on 18 Mar 2018
+Small improvement:
Looking at my script again, I found any function in the 5th line is unnecessary. Please revise the line as follows.
idx = isnan(maxt);
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!