ReadTable Producing ExtraVar Headers

32 vues (au cours des 30 derniers jours)
Jamie Moon
Jamie Moon le 24 Mai 2021
Commenté : Arshey Dhangekar le 16 Juin 2021
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 commentaires
Siddharth Bhutiya
Siddharth Bhutiya le 24 Mai 2021
What MATLAB version are you using?
Jamie Moon
Jamie Moon le 24 Mai 2021
R2018a

Connectez-vous pour commenter.

Réponse acceptée

Jeremy Hughes
Jeremy Hughes le 24 Mai 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 commentaire
Jamie Moon
Jamie Moon le 24 Mai 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

Connectez-vous pour commenter.

Plus de réponses (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 24 Mai 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 commentaire
Arshey Dhangekar
Arshey Dhangekar le 16 Juin 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by