MATLAB Answers

ReadTable Producing ExtraVar Headers

10 ビュー (過去 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 件のコメント
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.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by