How to identify a string and copy the value from the text file

Hii ppl... can anyone suggest how to identify a string in a txt file... im using some instructions and it is working well... if a string something like 151.04 LMD[q=1,d=2] 99.9972 % .. it has to identify the line of the string and give me the value as 99.9972.. if the string is like 150.04 ROTOR TIMECONST 940 m Im able to get the value as 940.. but the brackets [] are giving me problem.. how to solve this?? please suggest me

 採用された回答

per isakson
per isakson 2015 年 4 月 28 日
編集済み: per isakson 2015 年 5 月 5 日

0 投票

This script
str = fileread( 'cssm.txt' );
cac = regexp( str, '(?<=LMD\[q=1,d=2\][ ]+)[\d\.]++(?=[ ]+\%)', 'match','once' )
cac = regexp( str, '(?<=ROTOR TIMECONST[ ]+)\d++(?=[ ]+m)' , 'match','once' )
returns
cac =
99.9972
cac =
940
where cssm.txt contains
Hii ppl... can anyone suggest how to identify a string
in a txt file... im using some instructions and it is
working well... if a string something like
151.04 LMD[q=1,d=2] 99.9972 % .. it has to identify
the line of the string and give me the value as 99.9972..
if the string is like 150.04 ROTOR TIMECONST 940 m Im able
to get the value as 940.. but the brackets [] are giving me
problem.. how to solve this?? please suggest me
Mostly, regex is fast enough
&nbsp
ADDENDUM
The script below does the "same" job orders of magnitude faster. (I guess, I did a beginners mistake.)
str = fileread( 'cssm.txt' );
cac = regexp(str, '(?<=LMD\[q=1,d=2\])[ \d\.]++(?=\%)', 'match','once')
cac = regexp(str, '(?<=ROTOR TIMECONST)[ \d]++(?=m)' , 'match','once')
returns
cac =
99.9972
cac =
940
The differences are that in this latter case
  • the spaces, which surrounds the value, are captured together with the value.
  • if there is only spaces between "ROTOR TIMECONST" and "m", this script captures the string of spaces
  • ...
This variant doesn't capture a string of spaces
cac = regexp(str, '(?<=ROTOR TIMECONST) +\d++ +(?=m)', 'match','once')
but is significantly slower.

4 件のコメント

madhu T S
madhu T S 2015 年 5 月 4 日
thank you so much per isakson... the given code is working perfectly.. thanks again
madhu T S
madhu T S 2015 年 5 月 5 日
Hi its taking long time to get the value... is there any other way to get the value??
thanks and regards Madhu
per isakson
per isakson 2015 年 5 月 5 日
編集済み: per isakson 2015 年 5 月 5 日
Yes, but faster .... . What's the fastest way depends heavily on the size and content of the file. BTW: what do you mean by slow?
I've added a faster script to my answer.
madhu T S
madhu T S 2015 年 5 月 18 日
Hi per isakson... actually slow in the sense... if I use the code to get the value once its working fine... i have 60 lines of data in which the braces [] present.. it taking long time to process these 60 lines and to give value for these 60 lines...

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

その他の回答 (1 件)

the cyclist
the cyclist 2015 年 4 月 28 日
編集済み: the cyclist 2015 年 4 月 28 日

0 投票

Here is one way, assuming that the value you are looking for is always between two spaces at the end of the string:
S = '150.04 ROTOR TIMECONST 940 m';
% Find where the spaces are
indexToSpaces = regexp(S,' ');
% Get the characters that are between that last two spaces
valueString = S(indexToSpaces(end-1)+1:indexToSpaces(end)-1);
% Convert those characters to numeric
value = str2double(valueString);
If that is not correct, then you need to think carefully about what rule will consistently identify where the value is. Then maybe we can help you translate that into MATLAB code.

1 件のコメント

madhu T S
madhu T S 2015 年 5 月 4 日
thanks for your answer the cyclist, Im using the same logic but its working for others except for 151.04 LMD[q=1,d=2] 99.9972 %, the sqare brackets making problem for the above code... However I got code from per isakson and its working well... thanks again for your feed back the cyclist

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

カテゴリ

ヘルプ センター および File Exchange で Holidays / Seasons についてさらに検索

質問済み:

2015 年 4 月 28 日

コメント済み:

2015 年 5 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by