Convert double to datetime when importing .csv
5 views (last 30 days)
Show older comments
Hello,
I have a .csv Log with date times stored as double value, similar to:
T =
1×4 table
X1 Y1 X2 Y2
________________ __ ________________ __
45335.7751723264 1 45335.7751723264 2
When using the import function and generating a template script I´ll subsequently get "double" VariableTypes.
The format for the datetime is dd/MM/uuuu HH:mm:ss.SSS
I´ve tried using setvaropts, mainly
% Specify InputFormat according to the datetime format
opts = setvaropts(opts, ["X1", "X2"], "InputFormat", "dd/MM/uuuu HH:mm:ss.SSS");
% Specify InputFormat as double
opts = setvaropts(opts, ["X1", "X2"], "InputFormat", "double", "DateTimeFormat", "dd/MM/uuuu HH:mm:ss.SSS");
% set VariableTypes to datetime and InputFormat in setvaropts
opts = setvartype(opts, ["X1", "X2"], "datetime");
opts = setvaropts(opts,["X1", "X2"], "DatetimeFormat", "dd/MM/uuuu HH:mm:ss.SSS", "DatetimeLocale", "de_DE");
and some other variations. But I get a warning that variables can´t be convertet to datetime.
I am currently using a function that iterates over the table to convert the values but it´s taking considerable time.
Is there a possibility to set the import options to convert the double values to the correct datetime format?
Thanks for your help!
3 Comments
Accepted Answer
Stephen23
on 15 Feb 2024
T = readtable('Trace.csv', 'Delimiter',';', 'VariableNamingRule','preserve')
P = wildcardPattern + "(X)";
F = @(n) datetime(n, 'ConvertFrom','excel');
T = convertvars(T,P,F)
More Answers (0)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!