readtable function ignores certain options
조회 수: 12 (최근 30일)
이전 댓글 표시
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
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?
답변 (1개)
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
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!