Import data csv with date/time and other info with setvaropts

38 ビュー (過去 30 日間)
Greg LANGE 2022 年 9 月 10 日
コメント済み: Greg LANGE 2022 年 9 月 12 日
I try to import a csv file into a table int the workspace.
My fist colonn in the csv is date with a time following : day/ month/ year hour minute.
The other collonns are datas that could be considered as double.
The code I am using is :
opts = detectImportOptions('essai_data_3.csv','Delimiter',';')
opts =
DelimitedTextImportOptions with properties: Format Properties: Delimiter: {';'} Whitespace: '\b\t ' LineEnding: {'\n' '\r' '\r\n'} CommentStyle: {} ConsecutiveDelimitersRule: 'split' LeadingDelimitersRule: 'keep' TrailingDelimitersRule: 'ignore' EmptyLineRule: 'skip' Encoding: 'UTF-8' Replacement Properties: MissingRule: 'fill' ImportErrorRule: 'fill' ExtraColumnsRule: 'addvars' Variable Import Properties: Set types by name using setvartype VariableNames: {'Var1', 'Var2', 'Var3' ... and 2 more} VariableTypes: {'datetime', 'double', 'double' ... and 2 more} SelectedVariableNames: {'Var1', 'Var2', 'Var3' ... and 2 more} VariableOptions: Show all 5 VariableOptions Access VariableOptions sub-properties using setvaropts/getvaropts VariableNamingRule: 'modify' Location Properties: DataLines: [1 Inf] VariableNamesLine: 0 RowNamesColumn: 0 VariableUnitsLine: 0 VariableDescriptionsLine: 0 To display a preview of the table, use preview
opts.VariableTypes = {'datetime','double','double','double','double'};
opts = setvaropts(opts,1,'DatetimeFormat','dd.MM.uuuu HH:mm:ss');
opts = setvaropts(opts,2:5,'DecimalSeparator',',');
T = readtable('essai_data_3.csv',opts);
Warning: The DATETIME data was created using format 'MM/dd/uuuu HH:mm' but also matched 'dd/MM/uuuu HH:mm'.
To avoid ambiguity, supply a datetime format using SETVAROPTS, e.g.
opts = setvaropts(opts,varname,'InputFormat','MM/dd/uuuu HH:mm');
T.Properties.VariableNames{1} = 'Time';
T.Properties.VariableNames{2} = 'Data1';
T.Properties.VariableNames{3} = 'Data2';
I tried to read the datasheet of "setvaropts" and use the answears from other questions but i didn't reached what I wanted and I get an error warning as my date format is not recognised because of an "'anbiguity" (PLEASE look at the orange warning)


Simon Chan
Simon Chan 2022 年 9 月 10 日
Try this:
opts = detectImportOptions('','Delimiter',';');
opts.VariableTypes = {'datetime','double','double','double','double'};
opts = setvaropts(opts,1,'InputFormat','dd/MM/uuuu HH:mm');
opts = setvaropts(opts,2:5,'DecimalSeparator',',');
T = readtable('',opts)
T = 2×5 table
Var1 Var2 Var3 Var4 Var5 ________________ ______ ____ ____ _______ 02/11/2016 17:51 256.45 0 0 0.13021 03/11/2016 17:53 260 1 3 25
  1 件のコメント
Greg LANGE 2022 年 9 月 12 日
thank you for the feed back !


その他の回答 (0 件)




Community Treasure Hunt

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

Start Hunting!

Translated by