MATLAB: MATLAB 2018a readtable VariableNamesLine bug

MATLABoptsreadtablevariablenamesline

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; %Defines the row location of channel variable name
opts.VariableUnitsLine = 4; %Defines the row location of channel units
opts.Delimiter =','; %Specifies that the data is comma seperated
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' ... and 81 more}
VariableTypes: {'datetime', 'double', 'double' ... and 81 more}
SelectedVariableNames: {'Header1', 'Var2', 'Var3' ... and 81 more}
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).

Best Answer

What is the result if you pass ReadVariableNames into the function?
T = readtable(fullFileName, opts, 'ReadVariableNames', true)
The readtable function defaults to using the variable names in the import options, so if you've set a variable name,
opts.VariableNames{4} = 'FOUR';
You'll see that reflected in the table without specifying ReadVariableNames.
If you change opts.VariableNamesLine and set ReadVariableNames=true you should get what you expect.