フィルターのクリア

How to read string/numeric line from file, and parse out one number?

4 ビュー (過去 30 日間)
David Pesetsky
David Pesetsky 2016 年 6 月 2 日
編集済み: dpb 2016 年 6 月 2 日
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.

採用された回答

dpb
dpb 2016 年 6 月 2 日
編集済み: dpb 2016 年 6 月 2 日
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 件のコメント
dpb
dpb 2016 年 6 月 2 日
編集済み: dpb 2016 年 6 月 2 日
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?
David Pesetsky
David Pesetsky 2016 年 6 月 2 日
It was the closing quote.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by