Good Day All,
I posed this question a long time ago which I can no longer find however there was no conclusive result anyhow I belive. I have taken the simplified the below script to only include the lines pertinent to this question. I am using MATLAB 2018a.
When using readtable to import a CSV file (or multiple) in to MATLAB the VariableNamesLine can be assigned to a numerical value (2,3,4 etc.) corresponding to the row in which the variables are contained in the CSV file. However even with VariableNamesLine set to 3 or any other value, readtable will only take the first row in the CSV file as the variables for the table.
opts=detectImportOptions('C:\Users\Documents\.....CSV');
opts.VariableNamesLine = 3;
opts.VariableUnitsLine = 4;
opts.Delimiter =',';
t = readtable('C:\Users\Documents\.....CSV',opts);
The documentation suggests that assigning the VariableNamesLine in such a manner is exactly how readtable should work.
FYI: I haven't specified DataLines opts seems to find that perfectly well.
"If you specify the ReadVariableNames argument in addition to opts the import options, then the readtable behavior changes based on the specification:
- If ReadVariableNames is true, then read the variable names from the specified file by using the VariableNamesRange or the VariableNamesLine property of the import options object.
- If ReadVariableNames is false, then read the variable names from the VariableNames property of the import options object."
When I check opts in the comand window, VariableNamesLine is set correctly per the below:
opts =
DelimitedTextImportOptions with properties:
Format Properties:
Delimiter: {','}
Whitespace: '\b\t '
LineEnding: {'\n' '\r' '\r\n'}
CommentStyle: {}
ConsecutiveDelimitersRule: 'split'
LeadingDelimitersRule: 'keep'
EmptyLineRule: 'skip'
Encoding: 'windows-1252'
Replacement Properties:
MissingRule: 'fill'
ImportErrorRule: 'fill'
ExtraColumnsRule: 'addvars'
Variable Import Properties: Set types by name using setvartype
VariableNames: {'Header1', 'Var2', 'Var3' ...
VariableTypes: {'datetime', 'double', 'double' ...
SelectedVariableNames: {'Header1', 'Var2', 'Var3' ...
VariableOptions: Show all 84 VariableOptions
Access VariableOptions sub-properties using setvaropts/getvaropts
Location Properties:
DataLines: [6 Inf]
VariableNamesLine: 3
RowNamesColumn: 0
VariableUnitsLine: 4
VariableDescriptionsLine: 0
To display a preview of the table, use preview
Curiously, the VariableUnitsLine parameter works perfectly well.
For reference a typical CSV File may have this format:
edit: Added 'opts' to read table where it was missing (typo).