How to stackplot a table?

4 Ansichten (letzte 30 Tage)
HWIK
HWIK am 21 Jul. 2021
Beantwortet: Peter Perkins am 27 Jul. 2021
Hi, I have a table of which I would like to make a stackedplot with each of its variables. I want to set the x axis as a datetime, but the table I want to plot is set up in a tricky way. Its variables are the following: start of a given process, end of that process (both as datetimes), and the remaining variables are the average values of a measurement taken of that process between the defined start and end. Where each row is a different run of that process (I have attached a sample mat file for further clarity).
What I want to plot is those mean values in the variables in a stackedplot, where the mean value would span from the start to the end of its respective row as a constant. I know my description is confusing, thats why I have also attached a rough sketch of what I want to get:
Hope that makes it clearer, any help is appreciated.
Thanks!

Akzeptierte Antwort

HWIK
HWIK am 21 Jul. 2021
Finally figured it out, just reorder the data, I did it like this:
load('example.mat')
example=table;
clear table
tstamp=vertcat(example.Start,example.End);
tstamp=sort(tstamp,1);
tabdata=example(:,[1,4:end]);
tabdata=vertcat(tabdata,tabdata);
tabdata.Num=[1:size(tabdata,1)]';
plot_data=join(table(tstamp,[1:size(tstamp,1)]','VariableNames',{'tstamp','Num'}),tabdata);
for i=1:size(DEC_segments.Prod,1)
plot_data(2*i-1,3:end)=example(i,4:end);
plot_data(2*i,3:end)=example(i,4:end);
end
plot_data=table2timetable(plot_data);
stackedplot(plot_data)

Weitere Antworten (2)

Konrad
Konrad am 21 Jul. 2021
Hi Oliver,
does this code produce the plot you need?
load('example.mat', 'table')
figure;
nvar = size(table,2)-3;
t = [table.Start table.End].';
vars = table.Properties.VariableNames;
for i = 1:nvar
subplot(nvar,1,i);
v = repmat(table.(vars{i+3}),1,2).';
plot(t(:),v(:));
title(vars{i+3});
end
Best, Konrad
  1 Kommentar
HWIK
HWIK am 21 Jul. 2021
Thanks for the answer Konrad, yes that is pretty much it. Just wanted to put it in a stackedplot for better visualisation. I posted an answer with my approach to that.

Melden Sie sich an, um zu kommentieren.


Peter Perkins
Peter Perkins am 27 Jul. 2021
That seems overly complicated. stack to the rescue?
>> t = table(datetime(2021,7,26,0:2:22,0,0)',datetime(2021,7,26,1:2:23,0,0)',rand(12,1),rand(12,1),'VariableNames',["Start" "Stop" "X" "Y"])
t =
12×4 table
Start Stop X Y
____________________ ____________________ _______ ________
26-Jul-2021 00:00:00 26-Jul-2021 01:00:00 0.55447 0.91873
26-Jul-2021 02:00:00 26-Jul-2021 03:00:00 0.98985 0.19437
26-Jul-2021 04:00:00 26-Jul-2021 05:00:00 0.97194 0.86681
26-Jul-2021 06:00:00 26-Jul-2021 07:00:00 0.30537 0.27956
26-Jul-2021 08:00:00 26-Jul-2021 09:00:00 0.36401 0.10247
26-Jul-2021 10:00:00 26-Jul-2021 11:00:00 0.31532 0.71339
26-Jul-2021 12:00:00 26-Jul-2021 13:00:00 0.67324 0.96444
26-Jul-2021 14:00:00 26-Jul-2021 15:00:00 0.37585 0.85541
26-Jul-2021 16:00:00 26-Jul-2021 17:00:00 0.45587 0.098504
26-Jul-2021 18:00:00 26-Jul-2021 19:00:00 0.13724 0.80801
26-Jul-2021 20:00:00 26-Jul-2021 21:00:00 0.72338 0.25643
26-Jul-2021 22:00:00 26-Jul-2021 23:00:00 0.9332 0.3889
>> ts = stack(t,["Start" "Stop"],'ConstantVariables',["X" "Y"])
ts =
24×4 table
X Y Start_Stop_Indicator Start_Stop
_______ ________ ____________________ ____________________
0.55447 0.91873 Start 26-Jul-2021 00:00:00
0.55447 0.91873 Stop 26-Jul-2021 01:00:00
0.98985 0.19437 Start 26-Jul-2021 02:00:00
0.98985 0.19437 Stop 26-Jul-2021 03:00:00
0.97194 0.86681 Start 26-Jul-2021 04:00:00
0.97194 0.86681 Stop 26-Jul-2021 05:00:00
0.30537 0.27956 Start 26-Jul-2021 06:00:00
0.30537 0.27956 Stop 26-Jul-2021 07:00:00
0.36401 0.10247 Start 26-Jul-2021 08:00:00
0.36401 0.10247 Stop 26-Jul-2021 09:00:00
0.31532 0.71339 Start 26-Jul-2021 10:00:00
0.31532 0.71339 Stop 26-Jul-2021 11:00:00
0.67324 0.96444 Start 26-Jul-2021 12:00:00
0.67324 0.96444 Stop 26-Jul-2021 13:00:00
0.37585 0.85541 Start 26-Jul-2021 14:00:00
0.37585 0.85541 Stop 26-Jul-2021 15:00:00
0.45587 0.098504 Start 26-Jul-2021 16:00:00
0.45587 0.098504 Stop 26-Jul-2021 17:00:00
0.13724 0.80801 Start 26-Jul-2021 18:00:00
0.13724 0.80801 Stop 26-Jul-2021 19:00:00
0.72338 0.25643 Start 26-Jul-2021 20:00:00
0.72338 0.25643 Stop 26-Jul-2021 21:00:00
0.9332 0.3889 Start 26-Jul-2021 22:00:00
0.9332 0.3889 Stop 26-Jul-2021 23:00:00
>> stackedplot(ts,["X" "Y"],'XVariable',"Start_Stop")

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by