Why do I receive an error when reading my file with the CSVREAD or DLMREAD functions from MATLAB?

2 views (last 30 days)
Why do I receive an error when reading my file with the CSVREAD or DLMREAD functions from MATLAB?
The CSVREAD and DLMREAD functions seems to ignore the column specification when reading in a file. I get errors when trying to read a non-numeric entry in column 1, even when I've specified to begin reading from column 2 (zero-based numbers). My file is as follows:
row1,row2,row3,row4,row5
col1,1,2,3,4
col1,5,6,7,8
col1,9,10,11,12
col1,13,14,15,16
The command I use is:
d = csvread('csvReadTest.csv',1,1)
Here is the error:
ERROR: ??? Trouble reading number from file (row 1, field 1) ==> col1,1,2,3,4\n
Error in ==> C:\MATLABR12\toolbox\matlab\iofun\dataread.dll
Error in ==> C:\MATLABR12\toolbox\matlab\iofun\textread.m
On line 159 ==> [varargout{1:nlhs}]=dataread('file',varargin{:});
Error in ==> C:\MATLABR12\toolbox\matlab\iofun\dlmread.m
On line 102 ==> result = textread(filename,'',nrows,'delimiter',delimiter,'whitespace',whitespace, ...
Error in ==> C:\MATLABR12\toolbox\matlab\iofun\csvread.m
On line 45 ==> m=dlmread(filename, ',', r, c);

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 10 Dec 2021
Edited: MathWorks Support Team on 13 Dec 2021
This bug has been fixed in MATLAB 6.1 (R12.1).
As a workaround, please consider pre-editing your data file to remove the first column of text. Alternatively, you can use TEXTREAD to read your file.
For example, consider the following file named 'test.txt':
row1,row2,row3,row4,row5
col1,1,2,3,4
col1,5,6,7,8
col1,9,10,11,12
col1,13,14,15,16
The following code will read the data into the variable 'x':
[x(:,1) x(:,2) x(:,3) x(:,4)] = textread('test.txt','%*s %d %d %d %d','delimiter',',','headerlines',1)

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!