How can I put comma seperated values from textscan into seperate cells?
7 views (last 30 days)
Show older comments
I'm new to MatLab and I'm having a great deal of trouble trying to do something I presume is very simple. I have a file that looks like the following
SLICEREDGAU20.DATA_ARRAY <_REAL>
DATA_ARRAY(200)
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1.4745595E-9,2.641462E-8,7.8520998E-7,1.6171925E-5,
6.2723091E-5,0.0002063214,0.0005570988,0.001315713,0.002751417,0.005247015,0.009616409,0.01683373,0.02844034,0.04643543,
0.07358865,0.1135271,0.170429,0.2491248,0.3551348,0.4946089,0.6731268,0.89572,1.165628,1.483803,1.848336,2.254024,
2.691879,3.149435,3.611472,4.060927,4.480292,4.853085,5.165209,5.406004,5.569412,5.654238,5.664005,5.606255,5.491678,
5.332707,5.142802,4.93486,4.720119,4.508523,4.308018,4.124142,3.960922,3.820675,3.705205,3.614858,3.549747,3.510638,
3.499047,3.518046,3.572296,3.668585,3.815859,4.024667,4.306351,4.671596,5.128528,5.680417,6.323534,7.045115,7.82268,
8.624056,9.408902,10.13173,10.74611,11.2093,11.48736,11.55897,11.41822,11.07539,10.55559,9.895544,9.139687,8.334854,
7.525967,6.751907,6.043154,5.420112,4.893212,4.464371,4.129119,3.878117,3.699773,3.58183,3.51283,3.483014,3.484908,
3.513266,3.564997,3.638327,3.732992,3.849389,3.98771,4.146922,4.324076,4.514336,4.711617,4.906785,5.0886,5.244713,
5.361861,5.427217,5.429628,5.360707,5.216126,4.995946,4.705107,4.353146,3.953501,3.52194,3.075702,2.631831,2.205435,
1.809134,1.452165,1.140133,0.8752089,0.6565893,0.4812391,0.344469,0.2405587,0.1638208,0.1086343,0.07010443,0.04393657,
0.02667431,0.01566725,0.008837543,0.004808377,0.002504107,0.001252895,0.0005905471,0.0002571121,0.000102026,3.1245392E-5,
3.8284497E-6,-2.2079069E-8,-3.4413159E-9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
End of Trace.
I can skip the Headers, but I need to put each of the comma seperated values into individual cells of an array, currently it is putting an entire line into a cell, my code currently looks like this
fid = fopen('rgdp20nl');
A = fgetl(fid);
A = fgetl(fid);
A = fgetl(fid);
for i=1:12;
A = fgetl(fid);
C = strtrim(A);
D(i) = textscan(C, '%f', 'delimiter', ',');
end
celldisp(D)
Any help or info would be greatly apreciated
1 Comment
Andrew Newell
on 11 Apr 2014
Can you edit the file so it looks right? It has some line breaks in the middle of numbers.
Answers (1)
Azzi Abdelmalek
on 11 Apr 2014
Edited: Azzi Abdelmalek
on 11 Apr 2014
fid = fopen('file.txt');
A = fgetl(fid);
A = fgetl(fid);
A = fgetl(fid);
for i=1:12
C{i} = fgetl(fid);
end
D=regexp(C,'[\w\+-]+(\.)?([\w\+-]+)?','match')
out=cell2mat(cellfun(@str2double,D,'un',0))
0 Comments
See Also
Categories
Find more on Text Files 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!