Extract time (hour+minute+second) from datetime vector

43 Ansichten (letzte 30 Tage)
Ariana
Ariana am 25 Okt. 2023
Verschoben: Dyuman Joshi am 25 Okt. 2023
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 Kommentare
Dyuman Joshi
Dyuman Joshi am 25 Okt. 2023
Could you please attach the data file? Use the paperclip button to attach.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Ariana
Ariana am 25 Okt. 2023
hi everyone,thank you to everyone who answered! I figured it out, and this is the code I used:
If datetimeBNF is the column with the original datetimes:
formattedDatetimes = cell(size(datetimeBNF));
for i = 1:numel(datetimeBNF)
formattedDatetimes{i} = datestr(datetimeBNF(i), 'HH:MM:SS');
end

Weitere Antworten (2)

Steven Lord
Steven Lord am 25 Okt. 2023
If you can use a datetime array instead of serial date numbers (or convert the serial date numbers to datetime):
rightnow = datetime('now')
rightnow = datetime
25-Oct-2023 18:07:00
use the timeofday function.
[timeSinceMidnight, midnight] = timeofday(rightnow)
timeSinceMidnight = duration
18:07:00
midnight = datetime
25-Oct-2023
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)
serialDateNumber =
739184.754871983
Then call datetime with an option:
rightnow2 = datetime(serialDateNumber, 'ConvertFrom', 'datenum')
rightnow2 = datetime
25-Oct-2023 18:07:00

Dyuman Joshi
Dyuman Joshi am 25 Okt. 2023
Verschoben: Dyuman Joshi am 25 Okt. 2023
Here's an approach -
in = load('example_array.mat')
in = struct with fields:
BNFratesall_NaN1: [21×2 double]
vec = in.BNFratesall_NaN1;
%Convert the dates to datetime()
dt = datetime(vec(:,1), 'ConvertFrom', 'datenum')
dt = 21×1 datetime array
07-Sep-2022 16:22:00 07-Sep-2022 16:23:59 07-Sep-2022 16:25:59 07-Sep-2022 16:27:59 07-Sep-2022 16:30:00 07-Sep-2022 16:32:00 07-Sep-2022 16:34:00 07-Sep-2022 16:36:00 07-Sep-2022 16:38:00 07-Sep-2022 16:40:00 07-Sep-2022 16:41:59 07-Sep-2022 16:43:59 07-Sep-2022 16:45:59 07-Sep-2022 16:48:00 07-Sep-2022 16:50:00 07-Sep-2022 16:52:00 07-Sep-2022 16:54:00 07-Sep-2022 16:56:00 07-Sep-2022 16:57:59 07-Sep-2022 16:59:59 07-Sep-2022 17:01:59
%Get the hour, minute and second values
[h,m,s] = hms(dt);
%Get the output as a duration() array
out = duration(h,m,s)
out = 21×1 duration array
16:22:00 16:23:59 16:25:59 16:27:59 16:30:00 16:32:00 16:34:00 16:36:00 16:38:00 16:40:00 16:41:59 16:43:59 16:45:59 16:48:00 16:50:00 16:52:00 16:54:00 16:56:00 16:57:59 16:59:59 17:01:59

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by