readtable function ignores certain options
10 views (last 30 days)
Show older comments
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
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?
Answers (1)
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
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.
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!