Search a text file for a particular string and then read all values in that line in which string is present into MATLAB

16 ビュー (過去 30 日間)
Luffy
Luffy 2013 年 10 月 21 日
コメント済み: Nom 2019 年 9 月 17 日
Example:
* T-Spice 14.11 Simulation Wed Sep 18 16:08:03 2013 E:\Stuff\Spice\SPICE PROGRAMS\New folder\ckt1\Cktspice.sp
* T-Spice Win32 14.11.20090811.05:10:58
*SEDIT: Alter=0
*SEDIT: Analysis types DCOP 1 ACMODEL 1 AC 0 TRANSIENT 0 TRANSFER 0 NOISE 0
* BEGIN NON-GRAPHICAL DATA
DC ANALYSIS - temperature=25.0
v = [2.0000e+000 1.0000e+000 1.0000e+000 1.0000e+000]
* END NON-GRAPHICAL DATA
I need to search the text file for character v & then matlab should take v as a vector.
  2 件のコメント
Luffy
Luffy 2013 年 10 月 21 日
編集済み: Luffy 2013 年 10 月 21 日
This is what i tried
fid = fopen('output.txt')
c = textscan(fid,'%s')
s = c{1}
lineIndex = strncmp(s, 'v', 3)
I get the index or line in which v is present but how do i pass variable v to Matlab
dpb
dpb 2013 年 10 月 21 日
doc input % and friends
I'd look at using an OS utility such as grep that returns the file name and line number for the given pattern. If needs be that you must do this inside Matlab
doc system

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

回答 (2 件)

Cedric
Cedric 2013 年 10 月 21 日
編集済み: Cedric 2013 年 10 月 21 日
content = fileread( 'myFile.txt' ) ;
tokens = regexp( content, 'v =\s*\[([^\]]+)', 'tokens' ) ;
values = sscanf( tokens{1}{1}, '%f' ) ;
where values is a column vector; transpose it if you need a row vector.
  2 件のコメント
Cedric
Cedric 2013 年 10 月 21 日
編集済み: Cedric 2013 年 10 月 21 日
Just for those who are interested in regular expressions, the pattern (2nd arg. in the call to REGEXP) used here defines the following: match..
  • the literal 'v ='
  • followed by zero or more white spaces: '\s' stands for any white space, and '*' means zero or more times (as many times as possible in fact) the expression which precedes directly the star
  • followed by a '[', which has to be escaped because it is a special character in regexp patterns (for defining sets of characters to match '[..]' or sets of characters not to match '[^..]')
  • followed by an expression which will match something that will become a token '( ... )'. The token is what we ask REGEXP to return when we specify 'tokens' as 3rd input arg. in the call
  • this something is one or more times the expression which precedes the '+' (same as '*' but starting at one), which is characters that are not in the set '[^ ... ]', the set being in fact just one character ']', which has to be escaped for the same reason we escaped '['. This is a way to take all characters, whichever they are, until the closing ']'.
Nom
Nom 2019 年 9 月 17 日
Thank you for explaining this thoroughly, really helps.

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


Anver Hisham
Anver Hisham 2017 年 5 月 18 日
You can use grepValues library,
v = grepValues('myFile.txt','v');

カテゴリ

Help Center および File ExchangeFiles and Folders についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by