Readtable and Readmatrix Ignore Specified Range and Produce Extra Variables
6 views (last 30 days)
Show older comments
I have this very simple code:
type 2025-03-28_02-07_10-0.csv
in1 = readtable('2025-03-28_02-07_10-0.csv', 'ReadVariableNames', false, 'Range', 'C2:C3')
in2 = readtable('2025-03-28_02-07_10-0.csv', 'ReadVariableNames', false, 'Range', 'C2:D3')
stupid_matlab = readtable('PATH', 'ReadVariableNames', false, 'Range', strcat('C2:D', string(ri_length)));
However, it completely ignores the column portion of the range I give it. Changing the number part of the specified range changes the number of rows, but it always produces four extra columns. If I specify C2:C3, I get five columns. If I specify C2:D3, I get six columns. I have not idea where it is getting the extra columns from. I have also tried "readmatrix" and it does the exact same thing. I've attatched the .csv, but I've opened it directly and in excel and don't see anything wrong.
0 Comments
Accepted Answer
Dyuman Joshi
on 20 Jun 2025
From what I have understood of your query, you need to specify that you don't require Extra columns -
in = readtable('2025-03-28_02-07_10-0.csv', 'ReadVariableNames', false)
%Specifying a range
in = readtable('2025-03-28_02-07_10-0.csv', 'ReadVariableNames', false, 'Range', 'B1:H5')
%Specifying a range with no extra variable creation
in = readtable('2025-03-28_02-07_10-0.csv', 'ReadVariableNames', false, 'Range', 'B1:H5', ...
'ExtraColumnsRule', 'ignore')
More Answers (2)
dpb
on 20 Jun 2025
d=dir('*.csv');
opt=detectImportOptions(d.name);
tin=readtable(d.name,'Range','C2:C3','ExtraColumnsRule','ignore')
I don't know if Mathworks would classify the behavior as a bug or not, but is unexpected, granted. Probably worth submitting a report as a quality of implementation issue. It's complicated, so not terribly surprising there are still some warts hanging about.
You can give it a little extra help as above to avoid the issue...
0 Comments
See Also
Categories
Find more on Graphics Object Programming 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!