Readtable and Readmatrix Ignore Specified Range and Produce Extra Variables

6 views (last 30 days)
I have this very simple code:
type 2025-03-28_02-07_10-0.csv
Meas. ID, Time/Date, WV, Index, Sort #1, Sort #2, Sort #3, , 3/28/2025 2:21:22 PM, 632.8, 1.4753 , , , , , 3/28/2025 2:22:10 PM, 632.8, 1.4754 , , , , , 3/28/2025 2:22:58 PM, 632.8, 1.4754 , , , , , 3/28/2025 2:23:46 PM, 632.8, 1.4755 , , , , , 3/28/2025 2:24:34 PM, 632.8, 1.4752 , , , , , 3/28/2025 2:26:16 PM, 632.8, 1.4757 , , , , , 3/28/2025 2:27:04 PM, 632.8, 1.4757 , , , , , 3/28/2025 2:27:52 PM, 632.8, 1.4757 , , , , , 3/28/2025 2:28:40 PM, 632.8, 1.4755 , , , , , 3/28/2025 2:29:28 PM, 632.8, 1.4756 , , , , , 3/28/2025 2:31:29 PM, 632.8, 1.4756 , , , , , 3/28/2025 2:32:17 PM, 632.8, 1.4756 , , , , , 3/28/2025 2:33:05 PM, 632.8, 1.4754 , , , , , 3/28/2025 2:33:53 PM, 632.8, 1.4756 , , , , , 3/28/2025 2:34:41 PM, 632.8, 1.4754 , , , , , 3/28/2025 2:36:08 PM, 972.3, 1.4685 , , , , , 3/28/2025 2:36:56 PM, 972.3, 1.4683 , , , , , 3/28/2025 2:37:44 PM, 972.3, 1.4687 , , , , , 3/28/2025 2:38:32 PM, 972.3, 1.4687 , , , , , 3/28/2025 2:39:20 PM, 972.3, 1.4684 , , , , , 3/28/2025 2:40:44 PM, 972.3, 1.4813 , , , , , 3/28/2025 2:41:32 PM, 972.3, 1.4683 , , , , , 3/28/2025 2:42:20 PM, 972.3, 1.4684 , , , , , 3/28/2025 2:43:07 PM, 972.3, 1.4685 , , , , , 3/28/2025 2:43:55 PM, 972.3, 1.4685 , , , , , 3/28/2025 2:45:19 PM, 972.3, 1.4679 , , , , , 3/28/2025 2:46:07 PM, 972.3, 1.4696 , , , , , 3/28/2025 2:46:55 PM, 972.3, 1.4700 , , , , , 3/28/2025 2:47:43 PM, 972.3, 1.4697 , , , , , 3/28/2025 2:48:31 PM, 972.3, 1.4697 , , , , , 3/28/2025 2:50:53 PM, 403.2, 1.4929 , , , , , 3/28/2025 2:51:41 PM, 403.2, 1.4925 , , , , , 3/28/2025 2:52:29 PM, 403.2, 1.4926 , , , , , 3/28/2025 2:53:17 PM, 403.2, 1.4928 , , , , , 3/28/2025 2:54:05 PM, 403.2, 1.4925 , , , , , 3/28/2025 2:55:26 PM, 403.2, 1.4922 , , , , , 3/28/2025 2:56:14 PM, 403.2, 1.4923 , , , , , 3/28/2025 2:57:02 PM, 403.2, 1.4923 , , , , , 3/28/2025 2:57:50 PM, 403.2, 1.4926 , , , , , 3/28/2025 2:58:38 PM, 403.2, 1.4925 , , , , , 3/28/2025 3:00:32 PM, 403.2, 1.4928 , , , , , 3/28/2025 3:01:20 PM, 403.2, 1.4926 , , , , , 3/28/2025 3:02:07 PM, 403.2, 1.4928 , , , , , 3/28/2025 3:02:55 PM, 403.2, 1.4927 , , , , , 3/28/2025 3:03:43 PM, 403.2, 1.4924 , , , , Mean, , , 1.4792 , Std Dev, , , .0100 , % Std Dev, , , 0.68, Count, , , 45, Min, , , 1.4679 , Max, , , 1.4929 ,
in1 = readtable('2025-03-28_02-07_10-0.csv', 'ReadVariableNames', false, 'Range', 'C2:C3')
in1 = 2×5 table
Var1 ExtraVar1 ExtraVar2 ExtraVar3 Var5 _____ _________ _________ _________ _____ 632.8 {' '} {' '} {' '} {' '} 632.8 {' '} {' '} {' '} {' '}
in2 = readtable('2025-03-28_02-07_10-0.csv', 'ReadVariableNames', false, 'Range', 'C2:D3')
in2 = 2×6 table
Var1 Var2 ExtraVar1 ExtraVar2 Var5 Var6 _____ ______ _________ _________ _____ _____ 632.8 1.4753 {' '} {' '} {' '} {' '} 632.8 1.4754 {' '} {' '} {' '} {' '}
stupid_matlab = readtable('PATH', 'ReadVariableNames', false, 'Range', strcat('C2:D', string(ri_length)));
Unrecognized function or variable '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.

Accepted Answer

Dyuman Joshi
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)
in = 51×8 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 __________ ______________________ _____ ______ ____ ____ ____ _____ {0×0 char} 03/28/2025 02:21:22 PM 632.8 1.4753 NaN NaN NaN {' '} {0×0 char} 03/28/2025 02:22:10 PM 632.8 1.4754 NaN NaN NaN {' '} {0×0 char} 03/28/2025 02:22:58 PM 632.8 1.4754 NaN NaN NaN {' '} {0×0 char} 03/28/2025 02:23:46 PM 632.8 1.4755 NaN NaN NaN {' '} {0×0 char} 03/28/2025 02:24:34 PM 632.8 1.4752 NaN NaN NaN {' '} {0×0 char} 03/28/2025 02:26:16 PM 632.8 1.4757 NaN NaN NaN {' '} {0×0 char} 03/28/2025 02:27:04 PM 632.8 1.4757 NaN NaN NaN {' '} {0×0 char} 03/28/2025 02:27:52 PM 632.8 1.4757 NaN NaN NaN {' '} {0×0 char} 03/28/2025 02:28:40 PM 632.8 1.4755 NaN NaN NaN {' '} {0×0 char} 03/28/2025 02:29:28 PM 632.8 1.4756 NaN NaN NaN {' '} {0×0 char} 03/28/2025 02:31:29 PM 632.8 1.4756 NaN NaN NaN {' '} {0×0 char} 03/28/2025 02:32:17 PM 632.8 1.4756 NaN NaN NaN {' '} {0×0 char} 03/28/2025 02:33:05 PM 632.8 1.4754 NaN NaN NaN {' '} {0×0 char} 03/28/2025 02:33:53 PM 632.8 1.4756 NaN NaN NaN {' '} {0×0 char} 03/28/2025 02:34:41 PM 632.8 1.4754 NaN NaN NaN {' '} {0×0 char} 03/28/2025 02:36:08 PM 972.3 1.4685 NaN NaN NaN {' '}
%Specifying a range
in = readtable('2025-03-28_02-07_10-0.csv', 'ReadVariableNames', false, 'Range', 'B1:H5')
in = 4×7 table
Var1 Var2 Var3 Var4 Var5 Var6 ExtraVar1 ______________________ _____ ______ ____ ____ ____ _________ 03/28/2025 02:21:22 PM 632.8 1.4753 NaN NaN NaN {' '} 03/28/2025 02:22:10 PM 632.8 1.4754 NaN NaN NaN {' '} 03/28/2025 02:22:58 PM 632.8 1.4754 NaN NaN NaN {' '} 03/28/2025 02:23:46 PM 632.8 1.4755 NaN NaN NaN {' '}
%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')
in = 4×6 table
Var1 Var2 Var3 Var4 Var5 Var6 ______________________ _____ ______ ____ ____ ____ 03/28/2025 02:21:22 PM 632.8 1.4753 NaN NaN NaN 03/28/2025 02:22:10 PM 632.8 1.4754 NaN NaN NaN 03/28/2025 02:22:58 PM 632.8 1.4754 NaN NaN NaN 03/28/2025 02:23:46 PM 632.8 1.4755 NaN NaN NaN

More Answers (2)

dpb
dpb on 20 Jun 2025
d=dir('*.csv');
opt=detectImportOptions(d.name);
tin=readtable(d.name,'Range','C2:C3','ExtraColumnsRule','ignore')
tin = 2×1 table
Var1 _____ 632.8 632.8
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...

Matt J
Matt J on 20 Jun 2025
Edited: Matt J on 20 Jun 2025
When I save as .xlsx, it seems to work okay,
in1 = readtable('2025-03-28_02-07_10-0.xlsx', 'ReadVariableNames', false, 'Range', 'C2:C3')
in1 = 2×1 table
Var1 _____ 632.8 632.8

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products


Release

R2025a

Community Treasure Hunt

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

Start Hunting!