ReadTable Producing ExtraVar Headers

34 visualizaciones (últimos 30 días)
Jamie Moon
Jamie Moon el 24 de Mayo de 2021
Comentada: Arshey Dhangekar el 16 de Jun. de 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 comentarios
Siddharth Bhutiya
Siddharth Bhutiya el 24 de Mayo de 2021
What MATLAB version are you using?
Jamie Moon
Jamie Moon el 24 de Mayo de 2021
R2018a

Iniciar sesión para comentar.

Respuesta aceptada

Jeremy Hughes
Jeremy Hughes el 24 de Mayo de 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 comentario
Jamie Moon
Jamie Moon el 24 de Mayo de 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

Iniciar sesión para comentar.

Más respuestas (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 24 de Mayo de 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 comentario
Arshey Dhangekar
Arshey Dhangekar el 16 de Jun. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Tables en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by