Error time series plot: cannot perform numeric operation which have non numerical data

Hello everybody!
I had a table object that I transformed to timeseries. When it comes to plot it, I get an error... How can I solve this issue?
Thank you very much!
prova4 = timeseries(mmean1416_pt); %1x1 table timeseries, in which time is categorical in the form Jan-2014
plot(prova4)
Error using timeseries/plot (line 47)
Cannot perform numeric operations on timeseries which have non-numeric data

 Accepted Answer

When you use timeseries() on a table object, then timeseries() does not look inside the table at the variables. It creates times 0:(rows-1) (seconds) where rows is the number of rows in the table. And the data associated with the time is the entire table row.
But table rows are not numeric, they are table() objects. And table() objects cannot be plotted.
If you have a table object that has a time field, you should consider creating a timetable(). There is no plot() for timetable objects, but you can access the Time property and whatever variable is appropriate.
Caution: If your times are categorical, then you have to be careful in how you construct the categorical in order to impose a sorting order and associated position values. Probably easier to datetime() with a 'convertfrom' to convert the categorical text into datetimes.

7 Comments

Thank you Walter for the clear explaination..
So is there another way to computer a monthly mean of my ts without convert it to a table?
So far I did this:
% time conversion
Time1416 = datestr(ts1416theta.Time); %start time is 01-Jan-2014 00:30:00, end time is 31-Dec-2016 23:30:00
mtx1416_pt = ts1416theta.Data;
Time = datenum(Time1416);
% from ts to tabletime
TT1416_pt = timetable(mtx1416_pt,'TimeStep',hours(1),'StartTime',datetime(2014,1,1));
mmean1416_pt = groupsummary(TT1416_pt,"Time","month","mean");
I now I wuold like to have a time series of these mmean values in order to plot and compare them with other data coming from another dataset
Why are you using datestr() and datenum() ? You do not use Time after you assign to it. Your Time is going through a numeric -> text -> numeric round-trip. If you had a reason to use datenum representation, why not just go directly to datenum ?
format long g
NOW = datetime('now')
NOW = datetime
25-Jun-2021 07:43:18
Nds = datestr(NOW)
Nds = '25-Jun-2021 07:43:18'
DNs = datenum(Nds)
DNs =
738332.321736111
DNd = datenum(NOW)
DNd =
738332.321737469
DNs - DNd
ans =
-1.35798472911119e-06
Basically the only "advantage" is that when you go to string and back, you round to the nearest second, for whatever benefit that gives you.
But you never use Time anyhow, so doesn't seem to be any point.
I am not clear on the reason for not converting to timetable?
TT1416_pt = ts2timetable(ts1416theta);
mmean1416_pt = groupsummary(TT1416_pt,"Time","month","mean");
or perhaps
mmean1416_pt = retime(TT1416_pt, "monthly", "mean")
Yes, I compute that code for time to check the start and the end..
Anyway I got an error:
TT1416_pt = ts2timetable(ts1416theta);
Undefined function 'ts2timetable' for input arguments of type 'timeseries'.
Unrecognized function or variable 'ts2timetable'.
The problem is that I need a timeseries of monthly mean data from my original time series (that are hourly observation) because I need to compare this data with timeseries coming from another data set.
If I have a timetable or a table as a final output I don't know what to do...
I mean, is there a way to compute monthly mean from my original timeseries of hourly observation?
having something like:
mmean = mean(ts1416theta, 1:720:end) %considering 30 (30*24) days in a month
And also, what should I do when I have months with 31 days? or february that has 28 or 29 days?
Sorry for bothering you with all these questions...
Ah it looks like ts2timetable() is R2021a; https://www.mathworks.com/help/matlab/ref/ts2timetable.html
TT1416_pt = timetable(ts1416theta.Data, 'RowTimes', ts1416theta.Time, 'VariableNames', {'ts1416theta'});
mmean1416_pt = retime(TT1416_pt, "monthly", "mean")
retime() knows about months being different lengths and will automatically categorize into the correct place.
an error again :(((
Error using timetable (line 470)
Row times must be datetime or duration vector.
What is the first entry in ts1416theta.Time ? Your comments earlier indicated it encodes 01-Jan-2014 00:30:00 but what is its class() and how does it show up when you display it?

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!