Plot Day of Year with Time

2 views (last 30 days)
Edgar
Edgar on 30 Nov 2022
Edited: Star Strider on 1 Dec 2022
This question was flagged by Star Strider
I'm trying to plot data with the headers 1-8 as my y-axis and the day of year with time in the x-axis, to show something similar as the "Example.png".
  2 Comments
KSSV
KSSV on 30 Nov 2022
In what format the time is?
Edgar
Edgar on 30 Nov 2022
Julian Date

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 30 Nov 2022
Edited: Star Strider on 1 Dec 2022
Try something like this —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1213723/Data_2.csv')
T1 = 49×9 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 ____________________ ______ ______ ______ ______ ______ ______ ______ ______ {'Time' } 1 2 3 4 5 6 7 8 {'242-23:00:57.216'} 4.0894 4.0915 4.0956 4.0935 4.0915 4.0956 4.0935 4.0925 {'242-23:03:21.016'} 4.0894 4.0915 4.0946 4.0946 4.0925 4.0967 4.0915 4.0925 {'242-23:05:45.716'} 4.0894 4.0904 4.0956 4.0935 4.0925 4.0967 4.0946 4.0925 {'242-23:08:09.416'} 4.0894 4.0904 4.0956 4.0935 4.0915 4.0946 4.0956 4.0925 {'242-23:10:34.116'} 4.0894 4.0894 4.0946 4.0925 4.0935 4.0967 4.0946 4.0925 {'242-23:12:58.816'} 4.0894 4.0925 4.0956 4.0925 4.0915 4.0967 4.0935 4.0925 {'242-23:15:22.516'} 4.0873 4.0915 4.0946 4.0946 4.0925 4.0967 4.0915 4.0946 {'242-23:17:46.216'} 4.0883 4.0915 4.0935 4.0946 4.0915 4.0977 4.0935 4.0946 {'242-23:20:10.916'} 4.0894 4.0904 4.0956 4.0935 4.0925 4.0967 4.0946 4.0925 {'242-23:22:35.616'} 4.0894 4.0904 4.0956 4.0935 4.0925 4.0967 4.0935 4.0925 {'242-23:25:00.316'} 4.0873 4.0915 4.0946 4.0946 4.0925 4.0977 4.0935 4.0946 {'242-23:27:24.016'} 4.0883 4.0915 4.0935 4.0946 4.0925 4.0956 4.0935 4.0946 {'242-23:29:47.716'} 4.0894 4.0904 4.0956 4.0946 4.0925 4.0967 4.0935 4.0925 {'242-23:32:12.416'} 4.0883 4.0915 4.0956 4.0935 4.0904 4.0956 4.0935 4.0925 {'242-23:34:37.116'} 4.0894 4.0915 4.0967 4.0935 4.0925 4.0967 4.0925 4.0935
V11 = extractBefore(T1.Var1,'-');
DOY = cellfun(@str2double,V11);
DOY = datetime([zeros(size(DOY,1),2)+[0 1] DOY]);
V12 = extractAfter(T1.Var1,'-');
TOD = datetime(V12, 'InputFormat','HH:mm:ss.SSS');
DT = DOY + timeofday(TOD);
DT.Format = 'MMM-dd HH:mm:ss.SSS';
% Check = day(DT,'dayofyear')
figure
plot(DT(2:end), T1{2:end,2:end})
grid
legend(compose('Var%d',2:size(T1,2)), 'Location','best')
T2 = T1;
T2 = addvars(T2, DT, 'Before','Var2');
T2 = removevars(T2,'Var1')
T2 = 49×9 table
DT Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 ___________________ ______ ______ ______ ______ ______ ______ ______ ______ NaT 1 2 3 4 5 6 7 8 Aug-29 23:00:57.216 4.0894 4.0915 4.0956 4.0935 4.0915 4.0956 4.0935 4.0925 Aug-29 23:03:21.016 4.0894 4.0915 4.0946 4.0946 4.0925 4.0967 4.0915 4.0925 Aug-29 23:05:45.716 4.0894 4.0904 4.0956 4.0935 4.0925 4.0967 4.0946 4.0925 Aug-29 23:08:09.416 4.0894 4.0904 4.0956 4.0935 4.0915 4.0946 4.0956 4.0925 Aug-29 23:10:34.116 4.0894 4.0894 4.0946 4.0925 4.0935 4.0967 4.0946 4.0925 Aug-29 23:12:58.816 4.0894 4.0925 4.0956 4.0925 4.0915 4.0967 4.0935 4.0925 Aug-29 23:15:22.516 4.0873 4.0915 4.0946 4.0946 4.0925 4.0967 4.0915 4.0946 Aug-29 23:17:46.216 4.0883 4.0915 4.0935 4.0946 4.0915 4.0977 4.0935 4.0946 Aug-29 23:20:10.916 4.0894 4.0904 4.0956 4.0935 4.0925 4.0967 4.0946 4.0925 Aug-29 23:22:35.616 4.0894 4.0904 4.0956 4.0935 4.0925 4.0967 4.0935 4.0925 Aug-29 23:25:00.316 4.0873 4.0915 4.0946 4.0946 4.0925 4.0977 4.0935 4.0946 Aug-29 23:27:24.016 4.0883 4.0915 4.0935 4.0946 4.0925 4.0956 4.0935 4.0946 Aug-29 23:29:47.716 4.0894 4.0904 4.0956 4.0946 4.0925 4.0967 4.0935 4.0925 Aug-29 23:32:12.416 4.0883 4.0915 4.0956 4.0935 4.0904 4.0956 4.0935 4.0925 Aug-29 23:34:37.116 4.0894 4.0915 4.0967 4.0935 4.0925 4.0967 4.0925 4.0935
VNT2 = cell(1,size(T1,2));
VNT2(1) = T1{1,1};
VNT2(2:end) = arrayfun(@(x){num2str(x)},T1{1,2:end}); % T2: Create & Assign New Variable Names, Eliminate First Row
T2.Properties.VariableNames = VNT2;
T2 = T2(2:end,:)
T2 = 48×9 table
Time 1 2 3 4 5 6 7 8 ___________________ ______ ______ ______ ______ ______ ______ ______ ______ Aug-29 23:00:57.216 4.0894 4.0915 4.0956 4.0935 4.0915 4.0956 4.0935 4.0925 Aug-29 23:03:21.016 4.0894 4.0915 4.0946 4.0946 4.0925 4.0967 4.0915 4.0925 Aug-29 23:05:45.716 4.0894 4.0904 4.0956 4.0935 4.0925 4.0967 4.0946 4.0925 Aug-29 23:08:09.416 4.0894 4.0904 4.0956 4.0935 4.0915 4.0946 4.0956 4.0925 Aug-29 23:10:34.116 4.0894 4.0894 4.0946 4.0925 4.0935 4.0967 4.0946 4.0925 Aug-29 23:12:58.816 4.0894 4.0925 4.0956 4.0925 4.0915 4.0967 4.0935 4.0925 Aug-29 23:15:22.516 4.0873 4.0915 4.0946 4.0946 4.0925 4.0967 4.0915 4.0946 Aug-29 23:17:46.216 4.0883 4.0915 4.0935 4.0946 4.0915 4.0977 4.0935 4.0946 Aug-29 23:20:10.916 4.0894 4.0904 4.0956 4.0935 4.0925 4.0967 4.0946 4.0925 Aug-29 23:22:35.616 4.0894 4.0904 4.0956 4.0935 4.0925 4.0967 4.0935 4.0925 Aug-29 23:25:00.316 4.0873 4.0915 4.0946 4.0946 4.0925 4.0977 4.0935 4.0946 Aug-29 23:27:24.016 4.0883 4.0915 4.0935 4.0946 4.0925 4.0956 4.0935 4.0946 Aug-29 23:29:47.716 4.0894 4.0904 4.0956 4.0946 4.0925 4.0967 4.0935 4.0925 Aug-29 23:32:12.416 4.0883 4.0915 4.0956 4.0935 4.0904 4.0956 4.0935 4.0925 Aug-29 23:34:37.116 4.0894 4.0915 4.0967 4.0935 4.0925 4.0967 4.0925 4.0935 Aug-29 23:37:00.816 4.0894 4.0925 4.0946 4.0925 4.0935 4.0967 4.0935 4.0925
TT2 = table2timetable(T2);
figure
hsp = stackedplot(TT2(2:end,:));
grid
VN2 = T2.Properties.VariableNames;
figure
tiledlayout(4,2)
for k = 1:size(T2,2)-1
nexttile
plot(T2{:,1}, T2{:,k+1})
grid
ylh = ylabel(VN2{k+1});
ylh.Rotation = 0;
ylh.VerticalAlignment = 'middle';
ylh.HorizontalAlignment = 'right';
ylh.FontWeight = 'bold';
end
% get(ylh)
As it turns out, stackedplot wants a timetable so I created ‘TT2’ for it.
Another option is tiledlayout and it came though successfully as well. I used a (4,2) layout here becaause otherwise they’re too small to see.
EDIT — Aesthetic fixes. Code unchanged.
EDIT — (1-Dec-2022 at 2:52).
Minor correction in ‘DOY’ calculation.
EDIT — (1 Dec 2022 at 12:54)
Created ‘VNT2’, updated ‘T2’.
.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!