readtable function ignores certain options

10 views (last 30 days)
Stane Lokar
Stane Lokar on 30 Mar 2018
Commented: Stane Lokar on 1 Apr 2018
Hello
I am trying to import a csv file into matlab for processing of data. I am using the readtable function, which works great, it just does not want to use the VariableNameLine option that I specify. It is using a line before it, that it automaticly detects as the variable name line, but it actualy has some additional info in it, which would make it more difficult to get that data out later. For example I want to get the name 'TIME', but what i get is 'TIME_S_DC_FU_3AC'
The code that I am using is:
file_name = 'the_name_of_my_file.csv';
opts = detectImportOptions(file_name)
opts.DataLine = 11;
opts.VariableNamesLine = 8;
data = readtable(file_name,opts)
the readtable functions seems to just ignore the VariableNamesLine option, which I am not sure why, as the DataLine options works
I also tried with xlsread, but some data is improrted as dates.
  2 Comments
dpb
dpb on 30 Mar 2018
Attach enough of the file to reproduce the problem; one would guess there's an issue of file formatting that's confusing line count.
What happens if you use
opts.VariableNamesLine = 9;
just for grins?
Stane Lokar
Stane Lokar on 30 Mar 2018
It does not change anything, although that row has the units in it.

Sign in to comment.

Answers (1)

Jeremy Hughes
Jeremy Hughes on 30 Mar 2018
READTABLE uses the variable names defined in the Options over the ones in the file. This is because you can re-use the options on multiple files.
file_name = 'the_name_of_my_file.csv';
opts = detectImportOptions(file_name);
opts.DataLine = 11;
opts.VariableNamesLine = 8;
data = readtable(file_name,opts,'ReadVariableNames',true)
Alternatively, you can set the values in opts.VariableNames to be anything you want.
You can also specify the number of header lines to detectImportOptions if you know the right value. Giving extra information often results in better detection.
opts = detectImportOptions(file_name,'NumHeaderlines',7);
  2 Comments
dpb
dpb on 1 Apr 2018
Edited: dpb on 1 Apr 2018
"READTABLE uses the variable names defined in the Options over the ones in the file"
Where is that documented, Jeremy? I couldn't find that stated anywhere.
Also, even when you physically tell it via the import options data to use
opts.VariableNamesLine=8;
it still doesn't set the 'ReadVariableNames' flag True seems a logical inconsistency to me. It's similar to HG where setting a 'XTickLabel' value also sets the 'XTickLabelMode' status to 'manual' from 'auto'
I'd think there ought to be a distinction between user-set variables in the option structure vis-a-vis those that came from the automagic file scan to eliminate this dichotomy; or at least a warning perhaps that the option conflicts with what's being used.
Stane Lokar
Stane Lokar on 1 Apr 2018
Setting the values in opts.VariableNames is not possible, as the files will not always have the same data in the same colums, so it's only possible to use the names that are already in the file, and it's the same with the header lines, there are not always the same number of header lines, as the files come from different sources.
I can try and tell you more when I get back to work in two days.
And just as dpb said I never saw anywhere in the help and documentation, that it would use the variable names defined in the Options over the ones in the file, but to me it sounds like plain logic, that it would do that, as opposed to what is happening here.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!