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

40 views (last 30 days)
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 Comments
dpb
dpb on 21 Oct 2013
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

Sign in to comment.

Answers (2)

Cedric Wannaz
Cedric Wannaz on 21 Oct 2013
Edited: Cedric Wannaz on 21 Oct 2013
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 Comments

Sign in to comment.


Anver Hisham
Anver Hisham on 18 May 2017
You can use grepValues library,
v = grepValues('myFile.txt','v');

Community Treasure Hunt

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

Start Hunting!