How to stackplot a table?
古いコメントを表示
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!
採用された回答
その他の回答 (2 件)
Konrad
2021 年 7 月 21 日
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
Peter Perkins
2021 年 7 月 27 日
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")
カテゴリ
ヘルプ センター および File Exchange で Tables についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!