Extract time (hour+minute+second) from datetime vector
44 ビュー (過去 30 日間)
古いコメントを表示
I have an array that has datetime in one column, and a rate in the second column. i want to make a plot that just has on the x axis, a 24 hour day, and I want to see how the data varies over the course of the day. My dataset is a month long - so I'd like to plot all of that data and visualize it by time of day.
i don't know how to extract just the hour+minute+second data in ONE column. I know I can get hour separately, minute separately, etc. but I just want a column that says 01:30:22 for example, for each datetime value I have. I've looked at the function datetime but I'm pretty sure that gives me day as well? I JUST want time in the output
For reference, my table (BNFrates) has datetime in this format: 738771.683333333
Thank you!
2 件のコメント
Dyuman Joshi
2023 年 10 月 25 日
Could you please attach the data file? Use the paperclip button to attach.
採用された回答
その他の回答 (2 件)
Steven Lord
2023 年 10 月 25 日
If you can use a datetime array instead of serial date numbers (or convert the serial date numbers to datetime):
rightnow = datetime('now')
use the timeofday function.
[timeSinceMidnight, midnight] = timeofday(rightnow)
To convert, first let's get the serial date number (you wouldn't need to do this, as you already have it. I need to do it so I can show you the results.)
format longg
serialDateNumber = datenum(rightnow)
Then call datetime with an option:
rightnow2 = datetime(serialDateNumber, 'ConvertFrom', 'datenum')
Dyuman Joshi
2023 年 10 月 25 日
移動済み: Dyuman Joshi
2023 年 10 月 25 日
Here's an approach -
in = load('example_array.mat')
vec = in.BNFratesall_NaN1;
%Convert the dates to datetime()
dt = datetime(vec(:,1), 'ConvertFrom', 'datenum')
%Get the hour, minute and second values
[h,m,s] = hms(dt);
%Get the output as a duration() array
out = duration(h,m,s)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Dates and Time についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!