How do I get detectImportOptions to preserve row order and not skip rows?

7 views (last 30 days)
I'm trying to read the attached textfile into a table so that I can call function row by row. I used the following import options ...
opt = detectImportOptions( ...
filepath, ...
'Filetype', 'text', ...
'VariableNamesLine', 1, ...
'Whitespace', ' ' ...
);
table = readtable(filepath, opt);
The output of readtable (attached, adfmap.mat) excludes some rows in the textfile and mixes up the ordering of the rows and I'm not sure why that is. I wrote a python script to check what was missing and the following rows containing these tokens were not imported:
"platform_vocabulary"
"sea_name"
"_NCProperties"
"creator_state"
"time_coverage_resolution"
"geospatial_vertical_max"
There are 110 lines (first line contains the 'Variable name" so 109 "data lines" total) but
table = readtable(filepath, opt);
returns 103 rows (again, with those 6 rows missing).
I created the textfile and used the following format:
  • 3 fields ('variables') per line
  • Comma as the delimiter
  • Each field is double-quoted (comma and the single quote might be used as string characters)

Accepted Answer

Minh Tran
Minh Tran on 16 Aug 2019
I took a subset of the larger textfile and included rows from the original textfile that went missing along with a couple of rows that were successfully imported and ran detectImportOptions against it.
I checked the properties of the output of detectImportOptions call and noticed that the DataLines attribute was different. In the original textfile (which included rows that were missing), the DataLines property was set to [8 Inf]. The smaller textfile had its DataLines property set to [2 Inf].
I made sure to set DataLines to [2 Inf] after calling detectImportOptions and this fixed the import issue.
opt = detectImportOptions( ...
adfmapfile, ...
'Filetype', 'text', ...
'TextType', 'char', ... % 'char' (default) or 'string'
'VariableNamesLine', 1, ...
'ExpectedNumVariables', 3, ...
'ReadVariableNames', true, ...% Treat first row as variable names
'Whitespace', ' \b\t', ...
'LineEnding', {'\n' '\r' '\r\n'}, ...
'Delimiter', {','} ...
);
opt.DataLines = [2 inf];
table = readtable(adfmapfile, opt);
MATLAB didn't import the rows out of order, it just ignored the first 8 lines for whatever reason.

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!