Readtable and Readmatrix Ignore Specified Range and Produce Extra Variables
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Dyuman Joshi
2025-6-20
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')
更多回答(2 个)
dpb
2025-6-20
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!