Effacer les filtres
Effacer les filtres

Plot multiple columns of table with datetime

48 vues (au cours des 30 derniers jours)
Julian
Julian le 18 Oct 2023
Commenté : dpb le 18 Oct 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?

Réponse acceptée

Voss
Voss le 18 Oct 2023
Modifié(e) : Voss le 18 Oct 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 commentaires
Voss
Voss le 18 Oct 2023
You're welcome!
dpb
dpb le 18 Oct 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

Connectez-vous pour commenter.

Plus de réponses (1)

dpb
dpb le 18 Oct 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

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by