Extracting time from datetime
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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.
.
Manas Pratap
2021 年 11 月 6 日
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!!
Star Strider
2021 年 11 月 6 日
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.
.
Manas Pratap
2021 年 11 月 6 日
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
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
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
2024 年 7 月 11 日
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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
