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

조회 수: 23 (최근 30일)
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
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 Wannaz
Cedric Wannaz 2013년 10월 21일
편집: Cedric Wannaz 님. 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

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


Anver Hisham
Anver Hisham 2017년 5월 18일
You can use grepValues library,
v = grepValues('myFile.txt','v');

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by