CSVREAD is returning a row of zeros for every other row

10 views (last 30 days)
I have a matrix titled 'S.csv' that is essentially a compilation of "survival ratios" for different age groups, so most of the values are just below 1. The first few columns are years and "group numbers", so I am making sure to leave those out when I call CSVREAD on this matrix.
Here is what I am entering, I am reading the matrix between rows 0 to 47 and 3 to 101:
S = csvread('S.csv',0,3, [0, 3, 47, 101])
For some unknown reason, this returns a matrix where every other row is all zeros. The non-zero rows all have their correct respective values, and "S" is returned as the correct size (48x99). I really can't figure out why every other row is just zeros.
Has anyone ever seen this happen with the CSVREAD function? I can't find any documentation on this.
  2 Comments
KAE
KAE on 22 Jan 2020
Edited: KAE on 5 Feb 2020
Yes, I had this problem too due to poor formatting. I worked around it by generating a function using the Import Data button.
dpb
dpb on 22 Jan 2020
@KAE -- read through the responses here carefully and you'll see the problem OP had is related to a badly formatted input file. In all likelihood your problem is, too. If that isn't enough of a hint to be able to find it in your case, post a new Question and include a sample of the file that errors, we can't diagnose what we can't see.

Sign in to comment.

Answers (3)

dpb
dpb on 14 Nov 2015
Your data were saved as a character string with a comma delimiter between fields within the strings. IOW, the first line looks like
"2014,1,1,0.995512232,0.9991681652,0.9996800384,0.9997750236,...
for each line which isn't a valid csv numeric format. You can get around (other than fixing the spreadsheet or other app that created it) by
>> x=textread('ben1.csv','','delimiter',',','whitespace','"');
>> whos x
Name Size Bytes Class Attributes
x 282x104 234624 double
>>
This uses the optional whitespace argument to tell textread to ignore the apostrophes in the file. textscan has the same facility.
  8 Comments
Ben Lockhart
Ben Lockhart on 15 Nov 2015
I was really overthinking that last question. The matrix has already been created by textread, so all I had to do was make a new matrix using subarrays by doing something like:
S=S(1:48,4:102);
dpb
dpb on 15 Nov 2015
Yes, I had shown that in earlier response before we determined the problem in the formatting of the input file.
I'd still suggest fixing that in the file-generation process is step one...

Sign in to comment.


dpb
dpb on 14 Nov 2015
Don't have your file for testing, but I think the RANGE argument is incorrect. Try
S = csvread('S.csv',[0, 3, 47, 101]);
Of course, it's probably just as simple to read the whole array and then just eliminate what you don't want.
S=csvread('S.csv'); % read the file
S=S(1:48,4:102); % select the subarray desired (salt to suit ranges, of course)
  2 Comments
Ben Lockhart
Ben Lockhart on 14 Nov 2015
I'm unable to just read the matrix as a whole. It gives this error:
Error using dlmread (line 138) Mismatch between file and format string. Trouble reading 'Numeric' field from file (row number 1, field number 1)
dpb
dpb on 16 Nov 2015
>> dlmread('ben.csv')
Error using dlmread (line 143)
Mismatch between file and format string.
Trouble reading number from file (row 1u, field 1u) ==>
"2014,1,1,0.995512232,0.9991681652,0.9996800384,0.9997750236,...
>>
BTW, if you had posted the full text of the error including the failed line that is echo'ed, we could have seen what the problem was then...which impresses that it is important to read ALL of the information pertaining to an error carefully...note there's the quote mark at the beginning of the line that is the culprit.

Sign in to comment.


Walter Roberson
Walter Roberson on 14 Nov 2015
Historically, csvread() and dlmread() could not be used for files that had any text in them at all, even in the header lines. That changed fairly recently, but it is possible that it still has bugs.
You should use textscan() instead:
skipcols = 3; %check whether this should be 2 or 3!
ncol = 99;
fmt = [repmat('%*s', 1, skipcols), repmat('%f', 1, ncol)];
fid = fopen('S.csv', 'rt');
datacell = textscan(fid, fmt, 'HeaderLines', 1, 'Delimiter', ',', 'CollectOutput', 1);
fclose(fid);
S = datacell{1};
  3 Comments
Walter Roberson
Walter Roberson on 14 Nov 2015
Could you attach the csv file for investigation?
Also, is it possible that the file was prepared on MS Windows but that you are reading it on OS-X or Linux?
Ben Lockhart
Ben Lockhart on 14 Nov 2015
Sure! Here is the link. If you could take a look at why this happens with CSVREAD and/or suggest a workaround, I would be very grateful.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!