Convert double to datetime when importing .csv
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Stephen23
le 15 Fév 2024
T = readtable('Trace.csv', 'Delimiter',';', 'VariableNamingRule','preserve')
P = wildcardPattern + "(X)";
F = @(n) datetime(n, 'ConvertFrom','excel');
T = convertvars(T,P,F)
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Tables dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!