Remove letters tabs and spaces from a string with numbers and letters while creating a vector with the remaining numbers
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
I have a file called "SSL.dat". The second line has some numbers I need but it's full of spaces and letters.
fidssl = fopen('SSL.txt','r');
line1ssl=fgetl(fidssl);
line2ssl=fgetl(fidssl); %line where the data I need is
This line has the following contents:
mass(Msun) 1.000 9.547922048e-4 2.858857575e-4 4.366245355e-5 5.151391279e-5 8.3459e-9 3.040433607e-6 1.660477111e-7 2.448326284e-6 3.227778035e-7 6.607313077e-9
There are spaces, not tabs, separating each column. I need to remove the letters and split each mass value into a vector so that the vector index matches the column number of said value.
For example, the value "1.000" would match mass(1) and so on.
Can anyone help?
5 commentaires
dpb
le 8 Mar 2017
Excepting if really is space-delimited file you've got a problem because the first line (after the header) you posted has one more column of data in it than the rest. You'll have to treat the two record types separately as there's no way to encode an empty blank field in C (and hence Matlab) input formatting rules, a very big weakness, unfortunately.
Stephen23
le 8 Mar 2017
Modifié(e) : Stephen23
le 8 Mar 2017
That is a very badly formatted file: it uses multiple spaces (and tabs) as the delimiter. Therefore in general a missing field cannot be detected using a simple delimiter (e.g. comma), nor by a fixed width because the combination of tabs and spaces actually means that each column has a different number of characters, depending on the row. Ouch.
Untested, but this should get you started:
opt = {'MultipleDelimsAsOne',true};
hdr = repmat('%s',1,12);
fmt = ['%12c%12c',repmat('%f',1,10)];
fid = fopen('SSL.txt','rt');
H = textscan(fid,hdr,1,opt{:});
C = textscan(fid,fmt,opt{:});
fclose(fid);
Réponse acceptée
Frederico Arez
le 8 Mar 2017
1 commentaire
dpb
le 8 Mar 2017
See alternative solution that reads the file directly; returns eleven (11) elements/row with NaN missing-value indicator for records past the first. I'm a little surprised; not sure precisely why this is...
Plus de réponses (1)
dpb
le 8 Mar 2017
Actually, to my surprise textscan did a better job than I expected...
>> v=mat2cell(textscan(fid,['%*s' repmat('%f',1,11)],'headerlines',1,'collectoutput',1))
v =
1.0000 0.0010 0.0003 0.0000 0.0001 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
5.2043 9.5820 19.2294 30.1036 68.1435 1.0000 0.3871 0.7233 1.5237 39.2644 NaN
0.0488 0.0557 0.0444 0.0112 0.4325 0.0167 0.2056 0.0068 0.0933 0.2447 NaN
1.3046 2.4853 0.7726 1.7680 43.7407 0.0001 7.0050 3.3946 1.8499 17.1514 NaN
188.1847 320.3479 142.9558 267.7650 194.2978 357.5452 174.7959 50.0115 19.3565 15.0233 NaN
275.0659 336.0136 96.5417 265.6496 150.8411 322.6181 29.1243 55.1860 286.5374 113.7629 NaN
100.4917 113.6430 73.9893 131.7939 36.1284 140.2998 48.3305 76.6678 49.5620 110.2869 NaN
>> v(1,end)
ans =
6.6073e-09
>>
0 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!