CSVREAD is returning a row of zeros for every other row
10 views (last 30 days)
Show older comments
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
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.
Answers (3)
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
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...
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
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.
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
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?
See Also
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!