CSVREAD is returning a row of zeros for every other row
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
dpb
2020-1-22
@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.
回答(3 个)
dpb
2015-11-14
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 个评论
dpb
2015-11-15
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
2015-11-14
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 个评论
dpb
2015-11-16
>> 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
2015-11-14
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 个评论
Walter Roberson
2015-11-14
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Data Preparation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!