MATLAB Answers

How do I extract the HH:MM:SS.FFF portion of a julian day time stamp?

6 ビュー (過去 30 日間)
Brad
Brad 2020 年 4 月 9 日
コメント済み: Brad 2020 年 4 月 13 日
I have a 2x1 cell array containing the following data;
Data_Time_Stamps_Cells =
' 086 2020 18:30:19.578'
' 086 2020 18:30:18.569'
I'm attempting to extract the HH:MM:SS.FFF portion of these time stamps using the following:
exp = '([\d:\.]+)';
times = regexp(Data_Time_Stamps_Cells, exp, 'tokens');
The result is a 2 x 1 cell array;
times =
'086' '2020' '18:30:19.578'
'086' '2020' '18:30:18.569'
Why am I getting the entire contents of the Data Time Stamps Cells in 3 individual cells?

  0 件のコメント

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2020 年 4 月 9 日
編集済み: Walter Roberson 2020 年 4 月 9 日
times = regexp(Data_Time_Stamps_Cells, '\d{1,2}:\d{2}:\d{2}\.\d{3}', 'match', 'once');
But you might as well do
cellfun(@(D) D(end-11:end), Data_Time_Stamps_Cells, 'uniform', 0)
exp = '([\d:\.]+)';
[\d:\.] means any one character that is a digit or a colon or a period. The + after that pattern means one or more occurances.
In ' 086 ', the 0 matches a digit, the 8 matches a digit, the 6 matches a digit, the space after does not match a digit or colon or period. So '086' would be matched.

  3 件のコメント

Brad
Brad 2020 年 4 月 9 日
Walter, thanks for taking a look at this. I got a question regarding the 1st part of the expression: \d{1,2}:
\d - is any numeric digit; equivalent to [0-9]
{1,2} - matches between 1 and 2 numeric digits
Am I safe to assume this is a quantifier to account for a single or double digit hour?
Walter Roberson
Walter Roberson 2020 年 4 月 9 日
Yes, exactly, it is accounting for the possibility of single or double digit hour. If you can be certain that the hour is double digit you can change it to \d{2}
The cellfun I posted earlier (and just corrected) assumes double-digit hour.
If you want the parts broken out, hour separated from minute and so on, then
times = regexp(Data_Time_Stamps_Cells, '(\d{1,2}):(\d{2}):(\d{2}\.\d{3})', 'tokens', 'once');
would return a size(Data_Time_Stamps_Cells) cell array, each entry of which is a 1 x 3 cell of character vectors with the parts broken out.
Somes the easiest approach is just to datetime()
[H,M,S] = hms(datetime(Data_Time_Stamps_Cells, 'InputFormat', 'DDD uuuu HH:mm:ss.SSS'));
Now H is a vector of (numeric) hours, M is numeric minutes, S of seconds.
Brad
Brad 2020 年 4 月 13 日
I forgot all about datetime. Thanks again Walter!

サインインしてコメントする。

その他の回答 (1 件)

Kelly Kearney
Kelly Kearney 2020 年 4 月 9 日
An alternative method that avoids regular expressions would be to let datetime to the parsing for you:
Data_Time_Stamp_Cells = {...
' 086 2020 18:30:19.578'
' 086 2020 18:30:18.569'};
% To datetime...
t = datetime(Data_Time_Stamp_Cells, 'inputformat', 'DDD uuuu HH:mm:ss.SSS');
% And back to your preferred format
datestr(t, 'HH:MM:SS.FFF') % Option A
char(t, 'HH:mm:ss.SSSS') % Option B
Using datestr (Option A) is a tiny bit faster than char (Option B) to convert back to a string, but then that adds in the annoyance of mixing datetime vs datenum/datestr formatting codes.

  0 件のコメント

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by