ReadTable Producing ExtraVar Headers

조회 수: 34 (최근 30일)
Jamie Moon
Jamie Moon 2021년 5월 24일
댓글: Arshey Dhangekar 2021년 6월 16일
Hi,
I am trying to import a *.CSV file with headers using readtable(). It works ok except that beyond the 6th column, it fails to recognize the header strings and just puts "ExtraVar[#]". I'm trying to figure out if there is something wrong with the CSV file I'm using or if I need to configure readtable differently.
opts = detectImportOptions(filename);
opts.VariableNamesLine = 1; % Look for headers in first line
opts.Delimiter = ',';
logData = readtable(filename,opts,'ReadVariableNames',true);
I've attached a truncated version of my CSV file. When I run the code on it above, it produces a table with 6 of the 8 headers correct but column 7 and 8 are called ExtraVar1 and ExtraVar2.
  댓글 수: 3
Siddharth Bhutiya
Siddharth Bhutiya 2021년 5월 24일
What MATLAB version are you using?
Jamie Moon
Jamie Moon 2021년 5월 24일
R2018a

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

채택된 답변

Jeremy Hughes
Jeremy Hughes 2021년 5월 24일
Main issue is that the sample code is a bad practice. Once created by detectImportOptions, the options don't get updated based on the file if you change a property. So if you're updating delimiter, you're still using all other detected parameters which were not based on that delimiter.
opts = detectImportOptions(filename); %<------ is probably detecting the wrong number of variables
opts.VariableNamesLine = 1; % Look for headers in first line
opts.Delimiter = ','; %<---- Changes the delimiter, but nothing else.
Try this instead of setting the delimiter after the fact.
opts = detectImportOptions(filename,'Delimiter',',');
detectImportOptions uses the parameters passed into it to do better detection. So instead of trying to guess the delimiter, it knows ',' is the anwser, so it detects the headerlines, variables, datatypes, etc. based on comma. It may also be faster.
  댓글 수: 1
Jamie Moon
Jamie Moon 2021년 5월 24일
Ah, that fixed it. Thanks.
I confused myself because I previously had the "'Delimiter',',' " name-value pair as arguments in readtable() but it does not work there. I overlooked that it could be placed as an argument in detectImportOptions().
Thanks again

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

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 5월 24일
Hi,
One more step is needed to get the numbers in double.
filename='sample_log_temp.csv';
opts = detectImportOptions(filename);
opts.VariableNamesLine = 1; % Look for headers in first line
opts.Delimiter = ',';
logData = readtable(filename,opts,'ReadVariableNames',true);
TIME=str2double(logData.UnixTime);
A_F=str2double(logData.AccelerometerFailure);
  댓글 수: 1
Arshey Dhangekar
Arshey Dhangekar 2021년 6월 16일
filename='WT_201120.csv';
opts = detectImportOptions(filename);
opts.VariableNamesLine = 1; % Look for headers in first line
opts.Delimiter = ',';
logData = readtable(filename,opts,'ReadVariableNames',true);
Hello it did not show reuquired header (Row 38 header in csv file) in Matlab using above code. Desired headers store number,time...till end.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by