Plot multiple columns of table with datetime

31 次查看(过去 30 天)
Hi, I want to plot multiple (all) columns of a table.
The x axis should be the datetime corresponding to the variables of the column.
My table is in this shape:
P = array2table(rand(288,3),'VariableNames', ["generator", "consumer", "storage"]);
How I was possible to plot all data was:
plot(fig.probax(3), P, P.Properties.VariableNames);
I have a datetime Array:
current_time = datetime('now','Format','yyyy-MM-dd HH:mm');
current_time.Minute = 15 * round(current_time.Minute/15);
datetimeArray = current_time + hours(0:0.25:3*24-2^(-8)).';
And I would like to have the datetimeArray as my x axis but I don't know how I use it in my plot function.
I already know how to show the datetime array in my x axis with
datetick(fig.probax(3),'x', 'HH:mm', 'keeplimits', 'keepticks');
How do I set the x axis of multiple columns of a table?

采纳的回答

Voss
Voss 2023-10-18
编辑:Voss 2023-10-18
P = array2table(rand(288,3),'VariableNames', ["generator", "consumer", "storage"]);
current_time = datetime('now','Format','yyyy-MM-dd HH:mm');
current_time.Minute = 15 * round(current_time.Minute/15);
datetimeArray = current_time + hours(0:0.25:3*24-2^(-8)).';
% ax = fig.probax(3);
ax = gca();
plot(ax, datetimeArray, P{:,:});
  3 个评论
dpb
dpb 2023-10-18
P = array2table(rand(288,3),'VariableNames', ["generator", "consumer", "storage"]);
current_time = datetime('now','Format','yyyy-MM-dd HH:mm');
current_time.Minute = 15 * round(current_time.Minute/15);
datetimeArray = current_time + hours(0:0.25:3*24-2^(-8)).';
% ax = fig.probax(3);
hAx=axes;
plot(hAx, datetimeArray, P.Variables); % alternative syntax

请先登录,再进行评论。

更多回答(1 个)

dpb
dpb 2023-10-18
Use a @doc:timetable instead and then geth stackedplot for free that does it all for you. Use the target figure/panel to place it where you wish in the app; I just let it default for demo as can't create uifgure here.
V=rand(288,3);
current_time = datetime('now','Format','yyyy-MM-dd HH:mm');
current_time.Minute = 15 * round(current_time.Minute/15);
datetimeArray = current_time + hours(0:0.25:3*24-2^(-8)).';
ttP=array2timetable(V,'RowTimes',datetimeArray,'VariableNames', ["generator", "consumer", "storage"]);
stackedplot(ttP)
If you really, really must have the three on one plot, then
figure
subplot(2,1,1)
plot(ttP.Time,ttP{:,ttP.Properties.VariableNames}) % use {} dereferencing table
subplot(2,1,2) % or
plot(ttP.Time,ttP.Variables) % use all variables shorthand

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by