Plot multiple columns of table with datetime
38 次查看(过去 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?
0 个评论
采纳的回答
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
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
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!