How to convert datenum to datetime in a MATLAB Table

조회 수: 19 (최근 30일)
Shambhavi Adhikari
Shambhavi Adhikari 2020년 1월 20일
답변: Andrei Bobrov 2020년 1월 20일
Hello, I have a table with list Ids in numbers and corresponding datenum in which these Ids were occured. Can you please help me to convert these datenum to dates?
The table is like this:
ID_1 Dates_1 ID_2 Dates_2
2 737735.191331019 6 737735.182129630
3 737735.182013889 7 737735.141481482
I tried this:
for counter= 1:(size(table,1))
if mod(counter,2)==0
col_name=table.Properties.VariableNames{counter};
table{:,col_name}=datetime(table{:,col_name},'ConvertFrom', 'datenum');
end
end
And i got error like this:
The following error occurred converting from datetime to double:
Undefined function 'double' for input arguments of type 'datetime'. To convert from datetimes to numeric, first subtract off a datetime origin, then convert to numeric using the SECONDS, MINUTES, HOURS, DAYS, or YEARS functions.

답변 (2개)

Bhaskar R
Bhaskar R 2020년 1월 20일
Suppose T is your table variable
T.Dates_1 = datetime(T.Dates_1, 'ConvertFrom', 'datenum');
T.Dates_2 = datetime(T.Dates_2, 'ConvertFrom', 'datenum');

Andrei Bobrov
Andrei Bobrov 2020년 1월 20일
a=[2 737735.191331019 6 737735.182129630
3 737735.182013889 7 737735.141481482];
T = array2table(a,'V',{'ID_1','Dates_1','ID_2','Dates_2'});
T = [T,varfun(@(x)datetime(x,'ConvertFrom','datenum'),T,'InputVariable',[2,4])];
Tout = T(:,[1,5,3,6]);

카테고리

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