readtable function ignores certain options

조회 수: 12 (최근 30일)
Stane Lokar
Stane Lokar 2018년 3월 30일
댓글: Stane Lokar 2018년 4월 1일
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
dpb
dpb 2018년 3월 30일
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 2018년 3월 30일
It does not change anything, although that row has the units in it.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jeremy Hughes
Jeremy Hughes 2018년 3월 30일
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
dpb
dpb 2018년 4월 1일
편집: dpb 2018년 4월 1일
"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 2018년 4월 1일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by