Plot Time Sensitive Data plus a fit in the same plot

4 ビュー (過去 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 Center および File ExchangeCalendar についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by