Extracting time from datetime
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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

댓글 수: 1
Star Strider
2021년 11월 7일
.
채택된 답변
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
2021년 11월 6일
I tried converting my column containing the timestamp to a cell array to use your function above.
However, i get these square brackets, and the function doesnt work

Star Strider
2021년 11월 6일
It doesn’t work because they have to be read in as (or converted to) character arrays. They currently are not.
I’m not certain how they’re being read (imported) or the MATLAB version reading them. I would use readtablle or readmatrix to import them. That should automatically import them as datetime arrays, so the timeofday call would be the only call necessary to extract the times. If readtable or readmatrix get confused and have problems converting them, and so read them as character vector arrays, do something similar to what I did in the ‘ct’ assignment to convert them first.
Attaching/uploading the file (or a representative sample from it) to here, and listing the MATLAB version being used, would help.
.
I've attached a sample file below.
I imported the data using the import data option, I imported them as individual column vectors and plotted them. I've written the code below
x = humidity_site1;
y = timestamp;
hday1 = x(674:721);
hday2 = x(722:769);
hday3 = x(770:817);
hday4 = x(818:865);
hday5 = x(866:913);
tday1 = hour(y(675:722));
tday2 = hour(y(722:769));
tday3 = hour(y(770:817));
tday4 = hour(y(818:865));
tday5 = hour(y(866:913));
plot(tday1,hday1,tday2,hday2,tday3,hday3,tday4,hday4,tday5,hday5)
legend('Mar 15','Mar 16','Mar 17','Mar 18','Mar 19');
figure
plot(tday1,hday1)
x and y are the individual column vectors that I imported.
Matlab version : R2021a
Thank you for your replies and patience, its much appreciated!!
Try this —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/791964/sample.csv', 'VariableNamingRule','preserve')
T1 = 40×6 table
Var1 timestamp temperature_site1 humidity_site1 temperature_site2 humidity_site2
____ ____________________ _________________ ______________ _________________ ______________
0 {'01-03-2018 00:00'} NaN NaN 23.1 61.6
1 {'01-03-2018 00:30'} 21 68 22.9 61.1
2 {'01-03-2018 01:00'} 20 73 23 61.6
3 {'01-03-2018 01:30'} 20 73 22.9 62.5
4 {'01-03-2018 02:00'} 20 73 22.4 63.4
5 {'01-03-2018 02:30'} 20 70 22 63.8
6 {'01-03-2018 03:00'} 19 78 21.7 64.3
7 {'01-03-2018 03:30'} 19 78 21.5 64.7
8 {'01-03-2018 04:00'} 19 78 21.5 65.2
9 {'01-03-2018 04:30'} 19 78 21.1 66.5
10 {'01-03-2018 05:00'} 18 83 20.9 67.4
11 {'01-03-2018 05:30'} 17 81 20.7 68.2
12 {'01-03-2018 06:00'} 17 84.5 20.6 67.8
13 {'01-03-2018 06:30'} 17 88 20.3 69.1
14 {'01-03-2018 07:00'} 17 88 20.9 70.5
15 {'01-03-2018 07:30'} 17 88 20.3 69.5
T1.timestamp(end)
ans = 1×1 cell array
{'01-03-2018 19:00'}
T1.timestamp = datetime(T1.timestamp, 'InputFormat','dd-MM-yyyy HH:mm', 'Format','dd-MM-yyyy HH:mm')
T1 = 40×6 table
Var1 timestamp temperature_site1 humidity_site1 temperature_site2 humidity_site2
____ ________________ _________________ ______________ _________________ ______________
0 01-03-2018 00:00 NaN NaN 23.1 61.6
1 01-03-2018 00:30 21 68 22.9 61.1
2 01-03-2018 01:00 20 73 23 61.6
3 01-03-2018 01:30 20 73 22.9 62.5
4 01-03-2018 02:00 20 73 22.4 63.4
5 01-03-2018 02:30 20 70 22 63.8
6 01-03-2018 03:00 19 78 21.7 64.3
7 01-03-2018 03:30 19 78 21.5 64.7
8 01-03-2018 04:00 19 78 21.5 65.2
9 01-03-2018 04:30 19 78 21.1 66.5
10 01-03-2018 05:00 18 83 20.9 67.4
11 01-03-2018 05:30 17 81 20.7 68.2
12 01-03-2018 06:00 17 84.5 20.6 67.8
13 01-03-2018 06:30 17 88 20.3 69.1
14 01-03-2018 07:00 17 88 20.9 70.5
15 01-03-2018 07:30 17 88 20.3 69.5
T1.TimeOfDay = timeofday(T1.timestamp)
T1 = 40×7 table
Var1 timestamp temperature_site1 humidity_site1 temperature_site2 humidity_site2 TimeOfDay
____ ________________ _________________ ______________ _________________ ______________ _________
0 01-03-2018 00:00 NaN NaN 23.1 61.6 00:00:00
1 01-03-2018 00:30 21 68 22.9 61.1 00:30:00
2 01-03-2018 01:00 20 73 23 61.6 01:00:00
3 01-03-2018 01:30 20 73 22.9 62.5 01:30:00
4 01-03-2018 02:00 20 73 22.4 63.4 02:00:00
5 01-03-2018 02:30 20 70 22 63.8 02:30:00
6 01-03-2018 03:00 19 78 21.7 64.3 03:00:00
7 01-03-2018 03:30 19 78 21.5 64.7 03:30:00
8 01-03-2018 04:00 19 78 21.5 65.2 04:00:00
9 01-03-2018 04:30 19 78 21.1 66.5 04:30:00
10 01-03-2018 05:00 18 83 20.9 67.4 05:00:00
11 01-03-2018 05:30 17 81 20.7 68.2 05:30:00
12 01-03-2018 06:00 17 84.5 20.6 67.8 06:00:00
13 01-03-2018 06:30 17 88 20.3 69.1 06:30:00
14 01-03-2018 07:00 17 88 20.9 70.5 07:00:00
15 01-03-2018 07:30 17 88 20.3 69.5 07:30:00
vn = T1.Properties.VariableNames;
lgdvars = vn(3:6);
lgdvars = cellfun(@(x)strrep(x,'_','\_'), lgdvars, 'Unif',0); % 'escape' The Underscores
figure
plot(T1.TimeOfDay, T1{:,3}, 'DisplayName',lgdvars{1})
hold on
plot(T1.TimeOfDay, T1{:,4}, 'DisplayName',lgdvars{2})
plot(T1.TimeOfDay, T1{:,5}, 'DisplayName',lgdvars{3})
plot(T1.TimeOfDay, T1{:,6}, 'DisplayName',lgdvars{4})
hold off
grid
legend('Location','best')

figure
yyaxis left
plot(T1.TimeOfDay, T1{:,3}, 'DisplayName',lgdvars{1})
hold on
plot(T1.TimeOfDay, T1{:,5}, 'DisplayName',lgdvars{3})
hold off
ylabel('Temperature (°C)')
yyaxis right
plot(T1.TimeOfDay, T1{:,4}, 'DisplayName',lgdvars{2})
hold on
plot(T1.TimeOfDay, T1{:,6}, 'DisplayName',lgdvars{4})
hold off
ylabel('Humidity (%)')
grid
legend('NumColumns',2, 'Location','northoutside')

There is only one file and it appears to cover only one day. Two plotting options are provided.
.
Hi!, thank you so much for the reply ^_^
I just have a couple of questions -
lgdvars = vn(3:6);
lgdvars = cellfun(@(x)strrep(x,'_','\_'), lgdvars, 'Unif',0);
plot(T1.TimeOfDay, T1{:,3}, 'DisplayName',lgdvars{1})
What do these above lines do? Im a little hazy on that and I want to understand it completely.
Thank you!!
Manas Pratap
2021년 11월 6일
Actually, I need to plot the data according to the time for various days, so the code snippet that you've sent doesnt actually achieve that... It should look something like this

Star Strider
2021년 11월 6일
Those assignments retrieve the column headings (variable names) from the table and then use them to create the legend entries as 'DisplayName' values (after ‘escaping’ the underscores so that they display as underscores).
I only have one file. See if the Signal Processing Toolbox strips plot function will do what you want when more files are added. (There are likely other ways to work with that as well.) See the documentations ection on Import or Export a Sequence of Files if that is a problem.
I imagine that the dewpoint remains relatively constant, so the humidity rises appropriately as the temperature decreases. It might be worthwhile to calculate the dewpoint from the available information, and plot that as well. Just a thought.
.
Star Strider
2021년 11월 7일
If my Answer helped you solve your problem, please Accept it!
.
Stephen23
2021년 11월 7일
Manas Pratap's incorectly posted "Answer" moved here:
The answer is actually one of the comments below. Thank you so much @Star Strider for your effort and advice, helped me a lot!
Manas Pratap
2021년 11월 14일
@Star Strider I have attached the complete dataset. If I had to plot the temperature site_1 data for 5 different days, say (March 14-18) without inputting the data manually like i did before, is there a way to select the data daywise and then plot them? The x axis should be hours and the y axis temperature...with 5 different lines for the different days
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',:)
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
2024년 7월 11일
0 개 추천
Simple answer for getting time e.g. now but any array works
timeofday(datetime('now')));
카테고리
도움말 센터 및 File Exchange에서 Annotations에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
