Extracting time from datetime

조회 수: 231 (최근 30일)
Manas Pratap
Manas Pratap 2021년 11월 6일
답변: Alessandro Livi 2024년 7월 11일
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
Star Strider 2021년 11월 6일
Try this —
C = {'12/01/2000 01:00:00' 30; '13/01/2000 01:15:00' 45}
C = 2×2 cell array
{'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')
ct = 2×1 datetime array
12-Jan-2000 01:00:00 13-Jan-2000 01:15:00
Time = timeofday(ct)
Time = 2×1 duration array
01:00:00 01:15:00
.
  댓글 수: 12
Manas Pratap
Manas Pratap 2021년 11월 14일
I tried the below. However only 1 value is returned...i need all the rows pertaining to the date so that I can plot it hourwise..
T1 = readtable('weather_data_2sites.csv')
T1.timestamp = datetime(T1.timestamp, 'InputFormat','dd-MM-yyyy HH:mm', 'Format','dd-MM-yyyy HH:mm')
T1.date = datetime(T1.timestamp, 'InputFormat','dd-MM-yyyy HH:mm', 'Format','dd-MM-yyyy')
T1.TimeOfDay = timeofday(T1.timestamp)
T1.Properties.VariableNames
T_Mar15 = T1(T1.date == '24-06-2018',:)
Star Strider
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 = 5780×7 table
Var1 timestamp temperature_site1 humidity_site1 temperature_site2 humidity_site2 Var7 ____ ____________________ _________________ ______________ _________________ ______________ ____________________ 0 {'01-03-2018 00:00'} NaN NaN 23.1 61.6 {'01-01-1970 11:59'} 1 {'01-03-2018 00:30'} 21 68 22.9 61.1 {0×0 char } 2 {'01-03-2018 01:00'} 20 73 23 61.6 {0×0 char } 3 {'01-03-2018 01:30'} 20 73 22.9 62.5 {0×0 char } 4 {'01-03-2018 02:00'} 20 73 22.4 63.4 {0×0 char } 5 {'01-03-2018 02:30'} 20 70 22 63.8 {0×0 char } 6 {'01-03-2018 03:00'} 19 78 21.7 64.3 {0×0 char } 7 {'01-03-2018 03:30'} 19 78 21.5 64.7 {0×0 char } 8 {'01-03-2018 04:00'} 19 78 21.5 65.2 {0×0 char } 9 {'01-03-2018 04:30'} 19 78 21.1 66.5 {0×0 char } 10 {'01-03-2018 05:00'} 18 83 20.9 67.4 {0×0 char } 11 {'01-03-2018 05:30'} 17 81 20.7 68.2 {0×0 char } 12 {'01-03-2018 06:00'} 17 84.5 20.6 67.8 {0×0 char } 13 {'01-03-2018 06:30'} 17 88 20.3 69.1 {0×0 char } 14 {'01-03-2018 07:00'} 17 88 20.9 70.5 {0×0 char } 15 {'01-03-2018 07:30'} 17 88 20.3 69.5 {0×0 char }
T1.timestamp = datetime(T1.timestamp, 'InputFormat','dd-MM-yyyy HH:mm', 'Format','dd-MM-yyyy HH:mm')
T1 = 5780×7 table
Var1 timestamp temperature_site1 humidity_site1 temperature_site2 humidity_site2 Var7 ____ ________________ _________________ ______________ _________________ ______________ ____________________ 0 01-03-2018 00:00 NaN NaN 23.1 61.6 {'01-01-1970 11:59'} 1 01-03-2018 00:30 21 68 22.9 61.1 {0×0 char } 2 01-03-2018 01:00 20 73 23 61.6 {0×0 char } 3 01-03-2018 01:30 20 73 22.9 62.5 {0×0 char } 4 01-03-2018 02:00 20 73 22.4 63.4 {0×0 char } 5 01-03-2018 02:30 20 70 22 63.8 {0×0 char } 6 01-03-2018 03:00 19 78 21.7 64.3 {0×0 char } 7 01-03-2018 03:30 19 78 21.5 64.7 {0×0 char } 8 01-03-2018 04:00 19 78 21.5 65.2 {0×0 char } 9 01-03-2018 04:30 19 78 21.1 66.5 {0×0 char } 10 01-03-2018 05:00 18 83 20.9 67.4 {0×0 char } 11 01-03-2018 05:30 17 81 20.7 68.2 {0×0 char } 12 01-03-2018 06:00 17 84.5 20.6 67.8 {0×0 char } 13 01-03-2018 06:30 17 88 20.3 69.1 {0×0 char } 14 01-03-2018 07:00 17 88 20.9 70.5 {0×0 char } 15 01-03-2018 07:30 17 88 20.3 69.5 {0×0 char }
T1.TimeOfDay = timeofday(T1.timestamp);
T2 = T1(:,[8 3 4 5 6]) % Extract Variables Of Interest To New Table
T2 = 5780×5 table
TimeOfDay temperature_site1 humidity_site1 temperature_site2 humidity_site2 _________ _________________ ______________ _________________ ______________ 00:00:00 NaN NaN 23.1 61.6 00:30:00 21 68 22.9 61.1 01:00:00 20 73 23 61.6 01:30:00 20 73 22.9 62.5 02:00:00 20 73 22.4 63.4 02:30:00 20 70 22 63.8 03:00:00 19 78 21.7 64.3 03:30:00 19 78 21.5 64.7 04:00:00 19 78 21.5 65.2 04:30:00 19 78 21.1 66.5 05:00:00 18 83 20.9 67.4 05:30:00 17 81 20.7 68.2 06:00:00 17 84.5 20.6 67.8 06:30:00 17 88 20.3 69.1 07:00:00 17 88 20.9 70.5 07:30:00 17 88 20.3 69.5
T2 = fillmissing(T2,'nearest');
T2VN = T2.Properties.VariableNames(2:end)
T2VN = 1×4 cell array
{'temperature_site1'} {'humidity_site1'} {'temperature_site2'} {'humidity_site2'}
[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
Alessandro Livi 2024년 7월 11일
Simple answer for getting time e.g. now but any array works
timeofday(datetime('now')));

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by