Potential bug related to readtable's "VariableNamingRule"

54 views (last 30 days)
According to Create table from file - MATLAB readtable (mathworks.com), 'VariableNamingRule' and 'preserve' is a valid name-value pair. However, this pair does NOT seem to be recognized when there is an existing name-value pair in the argument. That is,
inertialMagFieldDataTable = readtable('InertialMagFieldData_simplified.csv', 'VariableNamingRule', 'preserve')
does work, but
inertialMagFieldDataTable = readtable('InertialMagFieldData_simplified.csv', 'VariableNamingRule', 'preserve', 'Format', '%{dd MMM yyyy HH:mm:ss.SSS}D%f%f%f')
does NOT work (and gives the error "Invalid parameter name: VariableNamingRule").
Question 1: is that a bug on MATLAB's part?
So, as a result, I've had to do something like
opts = detectImportOptions('InertialMagFieldData_simplified.csv');
opts.VariableNamingRule = 'preserve';
opts = setvartype(opts, 'Time_UTCG_', 'datetime');
opts = setvaropts(opts, 'Time_UTCG_', 'InputFormat', 'dd MMM yyyy HH:mm:ss.SSS', 'DatetimeFormat', 'yyyy-MM-dd HH:mm:ss');
inertialMagFieldDataTable = readtable('InertialMagFieldData.csv', opts);
I feel like there should be a way to do what the five lines of code above do with only one or two lines of code, especially if the potential bug related to 'VariableNamingRule' is fixed.
Question 2: what's the most concise way to do it (including converting the output datetime format to 'yyyy-MM-dd HH:mm:ss')?

Answers (1)

Walter Roberson
Walter Roberson on 20 Aug 2021
It is a bug or limitation up through R2021a, that when you use 'Format' the only acceptable other option is 'FileType'.


Find more on Dates and Time 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!