using regexp for strings with space delimited inside text file

1 回表示 (過去 30 日間)
sermet
sermet 2016 年 8 月 27 日
編集済み: per isakson 2016 年 8 月 27 日
I have a text file whose inside as follows;
ID: 01
Eccentricity: 0.5846023560E-002
Time of Applicability(s): 405504.0000
Orbital Inclination(rad): 0.9652538155
Rate of Right Ascen(r/s): -0.7828897534E-008
SQRT(A) (m 1/2): 5153.587402
Right Ascen at Week(rad): 0.2494223175E+001
Argument of Perigee(rad): 0.529637577
Mean Anom(rad): 0.1359485230E+001
I can extract particular string without space inside text as follows (from Azzi Abdelmalek answer);
fid=fopen('data.txt')
s=fgetl(fid)
out={}
while ischar(s)
out{end+1}=regexp(s,'(?<=(ID:))\s+\S+','match','once')
s=fgetl(fid);
end
fclose(fid)
idx=~cellfun(@isempty,out);
out=strtrim(reshape(out(idx),1,[]))'
But when it comes to other strings with space delimited (Time of Applicability(s): and the others) above codes don't work. How can I modify above codes to work consistently with space delimited strings?

採用された回答

per isakson
per isakson 2016 年 8 月 27 日
編集済み: per isakson 2016 年 8 月 27 日
It seems that : can be used delimiter between "label" and value.
I would read this file with
fid = fopen('data.txt');
cac = textscan( fid, '%s%f', 'Delimiter',':', 'Whitespace','' );
fclose(fid);
Inspect the result
>> cac{1}(3)
ans =
'Time of Applicability(s)'
>> cac{2}(3)
ans =
405504
Your code will work if you replace
'(?<=(ID:))\s+\S+'
by
'(?<=(:))\s+\S+'
"work consistently with space delimited strings" &nbsp space shouldn't be a problem. However, the parentheses, (), requires an escape character, \( and \), respectively.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by