Plot multiple columns of table with datetime

38 visualizaciones (últimos 30 días)
Julian
Julian el 18 de Oct. de 2023
Comentada: dpb el 18 de Oct. de 2023
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?

Respuesta aceptada

Voss
Voss el 18 de Oct. de 2023
Editada: Voss el 18 de Oct. de 2023
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 comentarios
Voss
Voss el 18 de Oct. de 2023
You're welcome!
dpb
dpb el 18 de Oct. de 2023
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

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 18 de Oct. de 2023
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

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by