How do I get MATLAB to read a long number as a date and time?

조회 수: 15 (최근 30일)
Isaac Cheng
Isaac Cheng 2022년 5월 17일
편집: Stephen23 2022년 5월 18일
So I have a long number which represents a time in this format mmddyyyyHHMMSS. Where mm is 2 digit month, dd is 2 digit day, yyyy is 4 digit year, HH is 2 digit hour, MM is 2 digit minute, and SS is 2 digit second. Example: 11142021092415 which is November 14th 2021 at 9:24:15.
How can I turn that long number into a date and time that matlab would understand, this is ultimately for the purpose of plotting a temperature value based on this time. The time was recorded this way because it spans over a few days.
I've tried looking into the datetime function and the like but with my level of skill and understanding, I am not sure how to execute it correctly. Greatly appreciate any help.
  댓글 수: 2
Dyuman Joshi
Dyuman Joshi 2022년 5월 17일
Is there any specific format you want to convert it to?
Stephen23
Stephen23 2022년 5월 17일
편집: Stephen23 2022년 5월 18일
That is a fragile, awful way to store a timestamp. Not only are the units in a mixed-up order, the statement about the numbers of digits "Where mm is 2 digit month" is incorrect because numeric types do not store leading zeros (as your example screenshot shows, which has a total of 13 digits per timestamp, so does not match your description). You are simply lucky that the solution proposed by Chris LaPierre using DATETIME seems to parse the variable number of digits of the first unit, and not the last unit (or any other unit).
You should avoid storing this as numeric. Prefer either text or DATETIME.

댓글을 달려면 로그인하십시오.

답변 (1개)

Cris LaPierre
Cris LaPierre 2022년 5월 17일
편집: Cris LaPierre 2022년 5월 17일
Convert your numbers to strings, and then use datetime to convert it to a time.
% original data
t = [11142021092415; 7242020093039];
% convert to string
T = string(t);
% Convert to datetime
d = datetime(T,'InputFormat','MMddyyyyHHmmss')
d = 2×1 datetime array
14-Nov-2021 09:24:15 24-Jul-2020 09:30:39
You can also set the display format if you want.
d.Format = 'MMMM dd, yyyy H:mm:ss'
d = 2×1 datetime array
November 14, 2021 9:24:15 July 24, 2020 9:30:39
  댓글 수: 1
Steven Lord
Steven Lord 2022년 5월 17일
Or if you're reading this data from a file, don't read it in as a double. Read it as a string (or directly into a datetime array, if you're using a function that supports that capability.)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by