How can I convert datetime into double?

조회 수: 247 (최근 30일)
bluebar
bluebar 2019년 12월 11일
댓글: Adam Danz 2024년 6월 19일
I have a data which is 60 x 4320 datetime, each cell is 2019/01/01 00:00:00 and want to convert this into 60 x 4320 double.
I tried to use datevec(data) but it resulted 259200 x 6 double, one row one character, like
2019 01 01 00 00 00
2019 01 01 00 10 00
2019 01 01 00 20 00
  댓글 수: 3
MatLab Code N
MatLab Code N 2021년 11월 17일
it is not possible to merge datetime with other double variables
Steven Lord
Steven Lord 2021년 11월 17일
You cannot have a double array where some of the elements are datetime values, that is true. But if you have date- and/or time-stamped data, I suggest using a timetable instead.
T = datetime('today');
randomDays = T + days(randi([-7 7], 10, 1));
x = (1:10).';
myTimedData = timetable(randomDays, x)
myTimedData = 10×1 timetable
randomDays x ___________ __ 15-Nov-2021 1 11-Nov-2021 2 16-Nov-2021 3 18-Nov-2021 4 13-Nov-2021 5 11-Nov-2021 6 22-Nov-2021 7 13-Nov-2021 8 15-Nov-2021 9 11-Nov-2021 10

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

답변 (3개)

galaxy
galaxy 2019년 12월 12일
You can use datenum to convert each your data to double.
Example:
>> t = datetime('now')
t =
datetime
2019/12/12 09:43:30
>> DateNumber = datenum(t)
DateNumber =
7.3777e+05
After that, if you want to convert back:
>> datetime(DateNumber,'ConvertFrom','datenum')
ans =
datetime
2019/12/12 09:43:30
hope it helps

Eric Sofen
Eric Sofen 2019년 12월 13일
What do you want the doubles to represent?
Datenum returns a double represents days since 0 CE.
The posixtime function would give you a double representing seconds since the Unix epoch (00:00:00 1-Jan-1970 UTC).
The juliandate function gives a double representing days since...
The trouble with using double to represent dates and times is that there isn't one definition (there are also precision-related issues over long spans of time).
I'm curious if you'd be willing to share what you want to do with the doubles, and why you can't do it using datetime?

Tianchu Lu
Tianchu Lu 2024년 6월 16일
편집: Adam Danz 2024년 6월 19일
Hi,
I have got the same problem, and I think I have managed to solve it.
This function does exactly what it is. Although this is for a cell array, just change {} brace bracket to () round bracket if your data type is not a cell type.
But please let me know if you are going to use it.
Thanks
Tian
function datetime_double_matrix = convertDatetimeCellArray(datetime_array)
% Convert a 1xN cell array of datetime strings to a 6xN double matrix
% in the order of [YYYY, MM, DD, HH, MN, SS]
% Initialize a matrix to store the date vectors
num_entries = length(datetime_array);
datetime_double_matrix = zeros(6, num_entries);
% Loop through the cell array and convert each datetime to a date vector
for i = 1:num_entries
% Convert the datetime string to a date vector
date_vector = datevec(datetime_array{i});
% Transpose the date vector and store it in the matrix
datetime_double_matrix(:, i) = date_vector';
end
end
  댓글 수: 1
Adam Danz
Adam Danz 2024년 6월 19일
You could also just use datevec(datetimeStrings)'
dtstr = {'12/10/2020 10:22:03','1/24/2021 22:00:01','9/9/1999'};
convertDatetimeCellArray(dtstr)
ans = 6x3
2020 2021 1999 12 1 9 10 24 9 10 22 0 22 0 0 3 1 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
datevec(dtstr).'
ans = 6x3
2020 2021 1999 12 1 9 10 24 9 10 22 0 22 0 0 3 1 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
function datetime_double_matrix = convertDatetimeCellArray(datetime_array)
% Convert a 1xN cell array of datetime strings to a 6xN double matrix
% in the order of [YYYY, MM, DD, HH, MN, SS]
% Initialize a matrix to store the date vectors
num_entries = length(datetime_array);
datetime_double_matrix = zeros(6, num_entries);
% Loop through the cell array and convert each datetime to a date vector
for i = 1:num_entries
% Convert the datetime string to a date vector
date_vector = datevec(datetime_array{i});
% Transpose the date vector and store it in the matrix
datetime_double_matrix(:, i) = date_vector';
end
end

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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by