How to read string/numeric line from file, and parse out one number?
2 views (last 30 days)
Show older comments
I can grab a specific line from a file, and store it. But however it's stored, I don't know how to pull out the 6th "value", and then use it as a number. Here's the file line:
259 FxR1 757.6469 17.6892 704.9626 820.0703 115.1077 kN
Here's how I pull and store:
fid2=fopen('file.txt');
C=textscan(fid2, '%s', 1, 'delimiter', '\n', 'headerlines', 463-1); %get one specific line
disp(C)
fclose(fid2);
Want to get the 820.0703 value out, and use that.
Thanks in advance.
0 Comments
Accepted Answer
dpb
on 2 Jun 2016
Edited: dpb
on 2 Jun 2016
Many ways, one would be
...
fmt=['%*f %*s' repmat('%*f',1,3) '%f']; % skip number/string/3numbers, read value
v=cell2mat(textscan(fid2,fmt,1,'headerlines', 463-1));
...
Alternatively, use the same format on the string read instead of the file.
3 Comments
dpb
on 2 Jun 2016
Edited: dpb
on 2 Jun 2016
Did you catch/fix the typo of a missing closing quote in the fmt string assignment after the %*s before repmat? Would think it wouldn't get as far as the assignment, but mayhaps it's confused the parser differently in a later release or from a script file rather than command line...
With l containing your line, and the corrected format string
>> fmt=['%*f %*s' repmat('%*f',1,3) '%f'];
>> v=cell2mat(textscan(l,fmt,1))
v =
820.0703
>>
Or, do you have v already defined as something else could be a possibility, I suppose...if the above doesn't fix it, what does
which v
return?
More Answers (0)
See Also
Categories
Find more on String Parsing 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!