Plot Time Sensitive Data plus a fit in the same plot

조회 수: 1 (최근 30일)
Tim Richter
Tim Richter 2021년 12월 8일
댓글: Tim Richter 2021년 12월 8일
Hello,
I imported two column vectors from an Excel sheet. One is called 'Thickness' and the other 'Date', which contains the Date of the measured thickness in the format: 'DD-MMM-YYY HH:MM:SS'.
I now want to achieve two things. To plot the thickness over time with a fit, and have the x-Axis displayed in the time format DD-MMM-YYY. I have no trouble with the plot, simply
plot(Date,Thickness,'.')
works fine. Then, i want to apply a general fit to the data, to see the overall tendency of thickness over the last year. I used
fit1 = fit(DateTime,Thickness,'poly1');
to create the fitted line. 'DateTime' is the Date vector, converted with
datenum(Date)
since the fit-function does not accept datetime variables. But the big problem is: How do I plot the fit and the thickness in one plot, AND let the x-axis be displayed in DD-MMM-YYYY? The plot won't work if the fit and the data have different types. Fit in 'datenum', Date in 'Datetime', aparrently can't be plotted simultaneously. I've tried hold on/off, with no luck.
It is possible to also convert the Date into datenum, but then the x-axis labels don't help in finding correct dates.
Thanks in advance!

답변 (1개)

Yongjian Feng
Yongjian Feng 2021년 12월 8일
To show more than one plots in a figure, use
plot(Date, Thickness, '.');
hold on % this means you want more than one plots in a figure
% plot the fit here
hold off
  댓글 수: 1
Tim Richter
Tim Richter 2021년 12월 8일
If I do it like this:
plot(Date,Thickness,'.');
hold on
plot(fit1,'-r') % or plot(Date,fit1,'-r') or plot(DateTime,fit1,'-r')
hold off
I get the error: 'Error evaluating CFIT function - Arguments must be numeric, char, or logical.'

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Exploration and Visualization에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by