MATLAB Answers

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
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
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.

Community Treasure Hunt

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

Start Hunting!