Extracting time from datetime
조회 수: 231 (최근 30일)
이전 댓글 표시
Hi,
How would I extract time from a timestamp? Say for example, I have a table with some 10 rows and say 5 columns.
Column 1 is the datestamp, for ex : 12/01/2000 01:00:00 and Column 2 is say Windspeed 30
so row1 looks like 12/01/2000 01:00:00 30
Let row 2 look like
13/01/2000 01:15:00 45 where 45 is the windspeed.
If I use the hours function, I get back only 01. But when I plot the two data with hours on the x axis, at the same point i get 2 values, which is not what I want. I want to plot two lines (for 12th and 13th) with y axis having the windspeed, and x axis having the hours. So if were to plot for say 10 days, i'd have 10 different lines with the y axis being the windspeed and x axis being the hour. But because Im only getting the "hour", for any windspeed values between 01:00:00 - 02:00:00, it all comes under the hour value of "1". I want it to be continuous.
This is what im getting, if you see the x axis has 0 to 24 values, but if the time is say 00:30, it plots at 0 itself and thats why the ugly graph
I'd want a graph like this
where its in hours and not minutes.
Example of the dataset im working with
채택된 답변
Star Strider
2021년 11월 6일
Try this —
C = {'12/01/2000 01:00:00' 30; '13/01/2000 01:15:00' 45}
ct = datetime(C(:,1), 'InputFormat','dd/MM/yyyy HH:mm:ss')
Time = timeofday(ct)
.
댓글 수: 12
Star Strider
2021년 11월 14일
This turned out to be a much more difficult problem that I at first thought it would be. The reason is that the data do not have consistent sizes, so plotting them against ‘TimeOfDay’ was not possible. I ended up plotting them as best I could, and then grafting the ‘TimeOfDay’ vector onto the plots using a set call, because that’s the only way I could make it work. I’m not certain how robust the code is, however it appears to work reasonably well for these data.
With that, this is my best effort —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/799489/weather_data_2sites1.csv', 'VariableNamingRule','preserve')
T1.timestamp = datetime(T1.timestamp, 'InputFormat','dd-MM-yyyy HH:mm', 'Format','dd-MM-yyyy HH:mm')
T1.TimeOfDay = timeofday(T1.timestamp);
T2 = T1(:,[8 3 4 5 6]) % Extract Variables Of Interest To New Table
T2 = fillmissing(T2,'nearest');
T2VN = T2.Properties.VariableNames(2:end)
[TOD,ixs,ixv] = unique(T2.TimeOfDay);
for k = 1:numel(T2VN)
aggvar{:,k} = accumarray(ixv,T2{:,k+1},[],@(x){x});
end
for k1 = 1:numel(aggvar)
figure
hold on
for k2 = 1:numel(aggvar)
plot(aggvar{k1}{k2})
end
hold off
grid
xt = linspace(0, numel(aggvar{1}{1}), numel(aggvar{1}));
set(gca,'XTick',xt, 'XTickLabel',string(TOD))
title(strrep(T2VN{k1},'_','\_'))
legend(compose('Day %d',1:4), 'Location','best')
end
The plots here are constrained and resize themselves to fit the window. They will probably look better when the code is run offline (that I did not do).
Experiment to get the different results, if desired.
.
추가 답변 (1개)
Alessandro Livi
2024년 7월 11일
Simple answer for getting time e.g. now but any array works
timeofday(datetime('now')));
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!