# How to convert datenum to datetime in a MATLAB Table

29 views (last 30 days)
Shambhavi Adhikari on 20 Jan 2020
Answered: Andrei Bobrov on 20 Jan 2020
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.

Bhaskar R on 20 Jan 2020
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 on 20 Jan 2020
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]);