Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to fgetl and sscanf numbers only

23 views
Skip to first unread message

summer8

unread,
Sep 24, 2012, 5:17:09 AM9/24/12
to

Hi,
I'm trying to read the numbers only from each line but without reading the ND or the commas.

ND, 1, -0.0104594 -0.12456164 6.0715322E-017
ND, 2, -0.0098119825 -0.12460658 -0.0013874772
ND, 3, -0.0096356887 -0.11458401 -0.001629882


With the numbers read, is it possible to sscanf them into another textfile and place the read numbers in this structure format?

<node id="1">-0.0104594,-0.12456164,6.0715322E-017</node>
<node id="2"> -0.0098119825,-0.12460658,-0.0013874772</node>
<node id="3">0.0096356887,-0.11458401,-0.001629882 </node>

dpb

unread,
Sep 24, 2012, 10:00:11 AM9/24/12
to
Use textscan() w/ 'delimiter',',' and skip the first field or use the
'treatasempty' parameter.

--



dpb

unread,
Sep 24, 2012, 12:40:40 PM9/24/12
to
...

Or, just go straightahead w/ fscanf/fprintf...

>> c={'ND, 1, -0.0104594 -0.12456164 6.0715322E-017';
'ND, 2, -0.0098119825 -0.12460658 -0.0013874772';
'ND, 3, -0.0096356887 -0.11458401 -0.001629882'};
>> reshape(sscanf(char(c)','ND, %d, %f %f %f'),4,[])'
ans =
1.0000 -0.0105 -0.1246 0.0000
2.0000 -0.0098 -0.1246 -0.0014
3.0000 -0.0096 -0.1146 -0.0016
>> fprintf('<node id="%d">%f,%f,%f</node>\n',ans')
<node id="1">-0.010459,-0.124562,0.000000</node>
<node id="2">-0.009812,-0.124607,-0.001387</node>
<node id="3">-0.009636,-0.114584,-0.001630</node>
>>


Salt to suit...NB the locations of the transpose operator...

--
0 new messages