MATLAB: Readtable is ignoring import options to get variable names

importingMATLABreadtablevariablenameline

Am I being stupid or is this function not logical?
I want to import a csv file. There are 3 header lines. The actual variable names are on line 3. The units are on line 2. Line 1 is to be ignored.
So if I just opts = detectImportOptions and then set opts.VariableNamesLine=3, opts.VariableUnitsLine=2. It picks up the latter and ignores completely the former and just uses the original variablenames it picked up on Line 1.
If I detectImportOptions(file,'NumHeaderLines',1) it then picks up the units line as the names.
If I do it again and tell it to skip 2 lines, it picks the right names. I can then set opts.VariableUnitsLine to 2 and it does go back and pick the units correctly.
So I do get what I want in the end. But the function doesn't seem to work as expected? i.e. create the options and then modify the line options. Seems like whatever it initially picks up first as the names gets set in stone and you can't do anything about it (except the sorta hacky way I just worked out).

Best Answer

This works with Jonas's testfile.txt. You need to specify that there are 3 header lines when you call detectImportOptions().
opts = detectImportOptions(filename, 'NumHeaderLines', 3);
opts.VariableNamesLine = 3;
opts.VariableUnitsLine = 2;
opts.VariableNames
c = readtable(filename, opts);