How to read string/numeric line from file, and parse out one number?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
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
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?
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!