Readtable and Readmatrix Ignore Specified Range and Produce Extra Variables
조회 수: 7 (최근 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!