ReadTable Producing ExtraVar Headers

18 views (last 30 days)
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 Comments
Siddharth Bhutiya
Siddharth Bhutiya on 24 May 2021
What MATLAB version are you using?

Sign in to comment.

Accepted Answer

Jeremy Hughes
Jeremy Hughes on 24 May 2021
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 Comment
Jamie Moon
Jamie Moon on 24 May 2021
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

Sign in to comment.

More Answers (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 24 May 2021
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 Comment
Arshey Dhangekar
Arshey Dhangekar on 16 Jun 2021
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.

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!