how to parse text file read into cell array

조회 수: 2 (최근 30일)
Jorge
Jorge 2024년 9월 27일
댓글: Voss 2024년 9월 27일
I'm trying to programmatically update a Qspice netlist. I read the *.net file in using fileread.
netlist = fileread([filepath filename);
the file looks like this:
'* netlist
L5 N08 0 {Lp} Rser = 0.01 ic=0
L6 0 N07 {Ls} Rser=0.01 ic=0
C3 N06 N08 {Cp}
C4 N07 N09 {Cs}
V2 N06 0 pulse -VGA VGA 0 3n 3n period/2 period ac=1
R1 N09 0 .01
L1 N11 0 {Lp} Rser = 0.01 ic=0
L2 0 N10 {Ls} Rser=0.01 ic=0
C1 N05 N11 {Cp}
C2 N10 N12 {Cs}
V1 N05 0 pulse -VGA VGA 0 3n 3n period/2 period
R2 N12 0 5
.tran 0 {duration} {starttran} 2n
.param fres1=83000
.param period= 1/fsw
.param Cp = 1/(Lp*(2*pi*fres1)^2)
.param Lp= 34µ
.param Ls= 34µ
.param Cp2 = Cp*2
.param Idc = 104.42
.
.param Vbat = 400
.param RL = Vbat/Idc
.param Rac = 8/pi^2*RL
.param Cs2 = Cs*2
.param Cs = 1/(Ls*(2*pi*fres2)^2)
k1 L5 L6 {kcoup}
.param duration =20m
.param starttran = duration-4m
.param VGA =40
.param kcoup=0.2
.param fres2=85000
.save I(L5) I(L6) v(n05) i(L1) i(R2)
k2 L1 L2 {kcoup}
.param startfreq = 82000
.param stopfreq = 87000
.param stepfreq = 50
.step param fsw list 81750 83000 85000 86400
.end
'
fileread brings this in as one big character array.
I want to programmatically replace the .step param line with a new one, as a loop through my code and update some of these parameters.
I can find the index into the char array where ".step param" occurs, but I can't figure out how to convert the characters from .step to the end of that line to a string so I can just replace the whole line with a new one, or to smartly count the characters from .step to the end of that line (those characters change, so I can't just look for a match) and just replace those characters in the array.
Thanks in advance for the help.

채택된 답변

Voss
Voss 2024년 9월 27일
filepath = '.';
filename = 'test.txt';
netlist = fileread(fullfile(filepath,filename));
% show the last part of netlist
disp(netlist(1003:end))
.param startfreq = 82000 .param stopfreq = 87000 .param stepfreq = 50 .step param fsw list 81750 83000 85000 86400 .end
new_stuff = '!!! HERE IS THE MODIFIED LINE - PUT WHATEVER YOU WANT HERE !!!';
netlist = regexprep(netlist,'\.step param.*',new_stuff,'dotexceptnewline');
% show the last part of netlist
disp(netlist(1003:end))
.param startfreq = 82000 .param stopfreq = 87000 .param stepfreq = 50 !!! HERE IS THE MODIFIED LINE - PUT WHATEVER YOU WANT HERE !!! .end
  댓글 수: 2
Jorge
Jorge 2024년 9월 27일
works like a charm!! thank you!
Voss
Voss 2024년 9월 27일
You're welcome!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by