Plotting Timetable in Matlab

load_demand=readtable("Demand_timeseries_1hourResolution.xlsx");
TR=table2timetable(load_demand);
Time=TR({'05-05-2020','05-06-2020 00:00:00'},:);
T = timetable2table(Time)
x=T{2:end,1}
y=T{2:end,3}
plot(x,y)
I am trying to plot my timetable, but it will only plot a single point for it. So instead of plotting the whole data series for 'Time' it only plots for the first point. I tried converting it to an array but that didn't work out too well. Hopefully somebody can help me

More Answers (2)

Duncan Po
Duncan Po on 19 Feb 2021
The line:
Time=TR({'05-05-2020','05-06-2020 00:00:00'},:);
only extracts two rows from TR. So Time only has two rows. Then when you define x and y, the first row is discarded, so x and y are scalar. That's the reason only one point is plotted.
If you want the entire series, just use the entire TR instead of extracting only two rows.

17 Comments

@Duncan Po I don't want to plot TR because that gives me the data for a whole year, where as Time gives me a table containing the data between the two dates, and that's what I'm iterested in plotting.
https://www.mathworks.com/help/matlab/ref/timerange.html#d122e1343230
@Walter Roberson Hey, i tried playing around with the timerange, and now just gives me an error for below script.
load_demand=readtable("Demand_timeseries_1hourResolution.xlsx");
demand = load_demand(timerange(start_date:end_date,3));
However, it also gives me an extra error now
Error using timerange (line 128)
TIMERANGE endpoints must be datetime or duration scalars, or text representing datetime or duration scalars.
Am I setting up it incorrectly?
load_demand = readtable("Demand_timeseries_1hourResolution.xlsx");
TR = table2timetable(load_demand);
subset = timerange(datetime('05-05-2020', 'InputFormat', 'MM-dd-yyyy'),datetime('05-06-2020 00:00:00', 'InputFormat', 'MM-dd-yyyy HH:mm:ss'));
Time = TR(subset,:);
T = timetable2table(Time)
x=T{2:end,1}
y=T{2:end,3}
plot(x,y)
By the way, why are you skipping the first row?
Note: using timerange would be a lot easier if you were using a standard way of representing the dates.
subset = timerange('2020-05-05', '2020-05-06')
The mess with calling datetime() with 'InputFormat' is only needed because month-day-year is not one of the recognized international time formats.
Anders Vigen
Anders Vigen on 20 Feb 2021
Edited: Anders Vigen on 20 Feb 2021
@Walter Roberson I think I'm close to getting it now. I just want my x array to also contain the hours from my T table, is that possible. Right now it is just a line at the date 05-05-2020, so it is taking all the values and plots it at the same date.
Should be okay provided that you have more than one hour of data being selected.
x = datetime('2020-05-05 00:00'):hours(1):datetime('2020-05-05 23:00');
y = rand(1,length(x));
plot(x, y)
@Walter Roberson how do I make the function for y use the values from column 3 of my T table? I have one value for each hour for the whole duration of may 5th.
load_demand = readtable("Demand_timeseries_1hourResolution.xlsx");
mask = isbetween(load_demand.Time, datetime('2020-05-05'), datetime('2020-05-06')-minutes(1));
x = load_demand.Time(mask);
y = load_demand{mask,3};
plot(x, y);
@Walter Roberson now it says
Unrecognized table variable name 'Time'.
Can the issue be that my Matlab is not up to date? I have the version 'MATLAB R2020b - academic use'
load_demand = readtable("Demand_timeseries_1hourResolution.xlsx");
mask = isbetween(load_demand{:, 1}, datetime('2020-05-05'), datetime('2020-05-06')-minutes(1));
x = load_demand.Time(mask);
y = load_demand{mask,3};
plot(x, y);
Walter Roberson
Walter Roberson on 21 Feb 2021
Edited: Walter Roberson on 21 Feb 2021
I do not have your file so I was guessing about the variable name based on some of your other comments in other posts.
@Walter Roberson when I use load_demand.Hours it gives me an error that say invalid plot. This refers to the column that only have hours. If I use load_demand.Date it plots of the data points for every hour of the 5th of may on a single line. load_demand.Date refers to the column only with dates. I think the real reason is that the datasheets dates and hours are sperated into two different column, and that is what gives the errors.
load_demand = readtable("Demand_timeseries_1hourResolution.xlsx");
load_demand.Time = load_demand.Date + hours(load_demand.Hours);
mask = isbetween(load_demand{:, 1}, datetime('2020-05-05'), datetime('2020-05-06')-minutes(1));
x = load_demand.Time(mask);
y = load_demand{mask,3};
plot(x, y);
This could have been resolved days ago if you have attached a sample file.
@Walter Roberson I do realise that now, and I'm sorry about that. I'm still quite new with Matlab and Mathworks. I attached the data file. The code you provided still gives an error, and I do believe it's due to that the hours in the datasheet is not numerical, I tried even to convert them to actual numbers but nothing happened.
The representation of hours turned out to be strange :(
load_demand = readtable("Demand_timeseries_1hourResolution.xlsx");
h = cellfun(@(S) sscanf(S, '%d', 1), load_demand.Hours);
load_demand.Time = load_demand.Date + hours(h);
mask = isbetween(load_demand{:, 1}, datetime('2020-05-05'), datetime('2020-05-06')-minutes(1));
x = load_demand.Time(mask);
y = load_demand{mask,3};
plot(x, y);
@Walter Roberson you are a genius! It worked completely as it should. Thank you very much for the dedication on this issue!

Sign in to comment.

Here I give a Example:
tt = datetime+years(1:10); % 比如从现在时间开始,连续十年时间点
y = rand(10,1);
TT = timetable(tt',y)
TT = 10x1 timetable
Time y ____________________ ________ 03-Jul-2025 07:22:39 0.73968 03-Jul-2026 13:11:51 0.65843 03-Jul-2027 19:01:03 0.83686 03-Jul-2028 00:50:15 0.87343 03-Jul-2029 06:39:27 0.074945 03-Jul-2030 12:28:39 0.71392 03-Jul-2031 18:17:51 0.67515 03-Jul-2032 00:07:03 0.21619 03-Jul-2033 05:56:15 0.28457 03-Jul-2034 11:45:27 0.080194
plot(TT.Time,y,LineWidth=2)

Categories

Products

Community Treasure Hunt

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

Start Hunting!