Extracting a number from an input (text file)?

in the middle of my program, I need to read a Number from a file (text or DAT). This is a part of the file
##$SUBNAM9= <"">
##$SW= 2776.7173944802
##$SWIBOX= (0..15)
0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0
##$SW_h= 833333.333333333
##$SWfinal= 0
##$TD= 16384
##$TD0= 1
##$TE= 300
##$TE2= 300
I want Matlab to first read this file, then search for the number in front of "##$TD= ", which is 16384 in this example. Then
TD = 16384;
Thanks in advance

 채택된 답변

Walter Roberson
Walter Roberson 2011년 5월 27일

0 개 추천

T = regexp(TheString, '^##\$TD=\s*(.*) \s*$', 'tokens');
Then T{1} will be the string of the number. str2double(T{1}) if you want the numeric value.

댓글 수: 8

Walter Roberson
Walter Roberson 2011년 5월 27일
If you have the input as a cell array of strings, then
linematches = strncmp(TheCellString,'%##$TD',5);
and then TheCellString{linematches} would be the full matching line. If you extract positions 7 to end and str2double() that, you would have the number.
Amin
Amin 2011년 5월 28일
Could you please help me again,
when I run string to double, I get this:
>> str2double(T{1})
??? Index exceeds matrix dimensions.
Walter Roberson
Walter Roberson 2011년 5월 28일
If the index exceeds the matrix dimensions the implication is that it found no matches.
What form is your TheString ? For regexp() it should either be a cell array of strings, or a single long string containing linefeeds (and possibly carriage returns) to mark the end of individual lines.
The code I showed does not do the reading from file. You could use it as
TheString = fileread('YourFile.txt');
T = regexp(TheString, '^##\$TD=\s*(.*) \s*$', 'tokens');
if isempty(T)
disp('string was not present')
else
disp(str2double(T{1}))
end
Amin
Amin 2011년 5월 29일
In fact I don't know what form my TheString is! :(
the complete code gives me this result:
string was not present
Oleg Komarov
Oleg Komarov 2011년 5월 29일
If you don't know that you want the number after:
##$TD=
then what are you asking for?
The question is wether the number follows "##$TD= " or "##$TD =" or something else.
Amin
Amin 2011년 5월 29일
I think it is clear in my question that the number follows "##$TD= "
Walter Roberson
Walter Roberson 2011년 5월 29일
Darn default behaviours...
T = regexp(TheString, '(?-s)(?m)^##\$TD=\s*(\S+)\s*$', 'tokens');
The exact pattern to use depends on whether you have lines that have embedded spaces and on whether you need to trim the trailing spaces from such lines.
Amin
Amin 2011년 5월 29일
Thanks Walter. Now working. Great!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Laura Proctor
Laura Proctor 2011년 5월 27일

0 개 추천

I'll put my answer here since it's a little different, but Walter's response is definitely a lot more elegant.
fid = fopen('data.txt');
dataText = fgetl(fid);
while ~feof(fid)
if strfind(dataText,'##$TD')
ldata = textscan(dataText,'##$%s %f');
TD = ldata{2};
break
end
dataText = fgetl(fid);
end
fclose(fid);

댓글 수: 2

Walter Roberson
Walter Roberson 2011년 5월 27일
strfind() used in that way would find ##$TD anywhere in the string, but the following textscan() would only match if the line started with $$# . The difference could potentially be a problem -- e.g., if the user had happened to use a comment that included ##$TD as part of it.
strncmp(dataText,'##$TD',5) would be more robust than strfind() in this matter.
Amin
Amin 2011년 5월 28일
This solution is much more understandable for basic people like me. But the 'one line' code from Walter Roberson is so elegant.
I use strncmp(dataText,'##$TD',5) instead of strfind() for this. Thank you Laura Proctor.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

질문:

2011년 5월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by