Plot Time Sensitive Data plus a fit in the same plot

1 view (last 30 days)
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!

Answers (1)

Yongjian Feng
Yongjian Feng on 8 Dec 2021
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 Comment
Tim Richter
Tim Richter on 8 Dec 2021
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.'

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!