MATLAB: Read one column of unknown length from CSV file

csvcsvreadtextscanundocumented

Let's say I want to read column C from a CSV file which has an unknown number of rows. I have written the following code:
csvread('Filepath',0,2,[0 2 Inf 2])
Which gives me the error:
Error using dlmread (line 157)
Internal size mismatch
Error in csvread (line 50)
m=dlmread(filename, ',', r, c, rng);
Have also tried:
csvread('Filepath',0,2,[0 2 : 2])
Which gives the error:
Attempted to access range(3); index out of bounds because numel(range)=2.
Error in dlmread (line 105)
if r > range(3) || c > range(4), result= []; return, end
Error in csvread (line 50)
m=dlmread(filename, ',', r, c, rng);
So any suggestions on how to accomplish this? Thanks

Best Answer

I failed. Since csvread is based on textscan, I propose you use textscan. It's at least better documented - IMO.
These two scripts read the third column of csv_test.txt.
fid = fopen( 'csv_test.txt', 'r' );
cac = textscan( fid, '%*d%*d%d%*d%*d%*d', 'Delimiter',',', 'Headerlines',1 );
fclose( fid );
fid = fopen( 'csv_test.txt', 'r' );
cac = textscan( fid, '%d%*[^\n]', 'Delimiter',',' ...
, 'Headerlines',1, 'Headercolumns',2 );
fclose( fid );
( 'Headercolumns',2 &nbsp is an undocumented option I stumbled upon when I tried to find out how to use csvread.)
and where csv_test.txt contains
A,B,C,D,E,F
11,12,13,14,15,16
21,22,23,24,25,26
31,32,33,34,35,36