Timetables not fully supported by plot tools?
Show older comments
Hello,
I am just wondering why when plotting a timetable with duration as time axis, The "basic fitting" and "data statistics" tool in plot windos are not available.

Does this mean that timetables have some limitations compared to arrays or tables? I am asking because I need to decide if timetable is the right data structure for my use case. Basically I am importing some measuremet datas from sensors acquisition which have timestamps. In the past I would have just used plain arrays, but I see that in modern Matlab timetables are somehow suggested for these use cases.
Accepted Answer
More Answers (1)
Let's say that with basic fitting, you wanted to fit a quadratic (y = a(1)*x^2+a(2)*x+a(3)) to your data. If the x data was double, this would be possible.
v = 0:5
y = polyval([1 2 3], v) % a would be [1 2 3]
A = polyfit(v, y, 2) % check that it is [1 2 3]
But with duration data for x, let's do a unit analysis. How would you add x^2 (with units of hours squared), x (with units of hours), and a constant together? I suppose you could make the constant a(1) be unitless, a(2) have units of hours, and a(3) have units of hours squared (which would require y to have units of hours squared) but then you couldn't store them all in one array.
In fact, the power operators aren't defined for duration data. Neither is the times operator where both operands are duration arrays. We don't let you make duration data with units of hours squared.
x = hours(v)
try
z = x.^2
catch ME
fprintf("Error: %s", ME.message)
end
x2 = hours([1 2; 3 4]);
try
z = x2^2
catch ME
fprintf("Error: %s", ME.message)
end
try
z = x.*x
catch ME
fprintf("Error: %s", ME.message)
end
So IMO it doesn't conceptually make sense to do basic fitting with date and time data.
The data statistics functionality could probably work with duration data. I haven't thought it completely through to identify any sticking points. My guess is that there haven't been (very many if any) requests for that tool to support datetime and duration data, so it hasn't been a priority.
4 Comments
"...doesn't conceptually make sense to do basic fitting with date and time data."
load census
f=fit(cdate,pop,'poly2') % 2nd order poly in time
subplot(3,1,1)
plot(f,cdate,pop)
xlabel('Year'), ylabel('Population')
xl1=xlim;
dur=cdate-cdate(1);
f=fit(dur,pop,'poly2') % 2nd order poly in duration
subplot(3,1,2)
plot(f,dur,pop)
xlim(xl1-cdate(1))
xlabel('Duration (Years)'), ylabel('Population')
Nothing conceptually wrong in fitting data against time values or even powers of times/durations. Simply must have tools that know how to handle a numeric representation -- here they're in doubles, of course, but one could explicitly use a duration and cast it back or the alternative of using the venerable datenum.
subplot(3,1,3)
d=years(cdate-cdate(1));
b=polyfit(years(d),pop,2);
plot(years(d),polyval(b,years(d)),'.-','markersize',10)
As for the dimensional analysis, it's no different conceptually than fitting against a temperature or any other physical unit as is done all the time -- of course the coefficients have corresponding inverse units of whatever is the output variable's unit over the corresponding power units of the term. Many well known correlations aren't even limited to using integral powers.
The issue is simply that Mathworks has not chosen to extend the datetime or duration classes to full numeric status; this is at least understandable given the representations aren't simple doubles, but it's one of implementation detail and extending all the other tools to handle the case, not a conceptual limitation. The workaround is that one must cast either class back into a standard numeric form that the tools can handle and in general hope the double has sufficient precision compared to the datetime/duration.
Luca
on 23 Jan 2026
dpb
on 23 Jan 2026
I posted some thoughts on that topic above -- I agree that if it is classic signal processing, particularly with fixed-rate sampling the array is likely a better choice although as noted there, there is the specific timeseries data class as well although it seems oriented towards events handling.
dpb
on 23 Jan 2026
ADDENDUM:
The comment by @Steven Lord regarding the units being messy is certainly true for displaying durations in anything other than powers of some fractional unit; the digital represntation in hh:mm:ss.SSS notation is probably simply not feasible for powers.
To do a duration would mean a conversion to a represntative numeric form and then retaining the associated powers of the one chosen unit as in the examples above are in years, years^2.
Categories
Find more on Time Series Events in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!