how to editing the value of a parameter in the textfile
Show older comments
hello people,
i've got that file with parameters:
Description
&CYCLE NAPP=14, NLIN=20, NCYCLE=2, NUMGEO=1, IF=97, NPRINT=4 &END
&SYSTEM SYSTYP='OPEN','CLOSED' &END
&APDATA NO=1, TYPE=21, APNAME='Fuel Cell', FCTYPE='SOFC-DIR',
EEQCOD= 2, PINAN= 1, DELPAN=0, TINAN= 700, PINCA= 1,
DELPCA=0, TINCA= 650, DCAC=0.9, PFCELL= 1, TFCELL= 800,
UFL= *0.7* , ESTMFL= 9, ESTMOX= 9 &END
&APDATA NO=2, TYPE=6, APNAME='Heat Exchgr.', PIN1= 1, DELP1=0,
TIN1= 20, TOUT1= 200, DELP2=0 &END
I need to change the value of UFL (marked value) and save the file again. Any idea how i could do that?
Thanks Cyros
6 Comments
Geoff Hayes
on 8 Jul 2014
Is there just one line in this file or multiple lines? Given the commas, is this a csv file of some sort?
Cyros
on 8 Jul 2014
Joseph Cheng
on 8 Jul 2014
Edited: Joseph Cheng
on 8 Jul 2014
open the file as read and writeable (without overwriting) read in the file line by line and using strfind() look for UFL=, then rewrite the line with the correct UFL value. this will only work if the number of characters of the correction are the same. Otherwise you'll have to read and write the rest of the text file.
Cyros
on 8 Jul 2014
Sara
on 8 Jul 2014
No strfind helps you locate the string. To read the file line by line, use fileline = fgetl(fid), where fid is the output of fopen
Jan
on 12 Jul 2014
I've removed the duplicate question. Please post a question only once.
Answers (1)
Joseph Cheng
on 8 Jul 2014
I created a text file using an Aesop's Fable "The Ant and the Grasshopper" and used the code below to swap out ant with ANT. since this was a quick code i don't handle the end of the file gracefully.
fid = fopen('example2.txt','r+');
n=0;
while 1
n=n+1;
tline = fgetl(fid)
if ~ischar(tline),break,end
ant = strfind(lower(tline), 'ant');
if ~isempty(ant)
tline(ant:ant+2) = 'ANT';
fseek(fid,-length(tline)-2,0);
fprintf(fid,'%s\r\n',tline)
% fseek(fid,2,0);
end
end
fclose(fid);
5 Comments
Cyros
on 8 Jul 2014
Joseph Cheng
on 8 Jul 2014
Edited: Joseph Cheng
on 8 Jul 2014
It is very APPLICATION specific. So make a backup before you try this out on actual data files. Basically I read in a line and check what i want to replace and replace that whole line. If i had more time i could have isolated the actual byte position of what was to be replaced. I hope you realize that you cannot just cut out a section of a file and have it squeeze back together so if you replace something longer or shorter everything after that would be affected. if you want to replace with something longer than you may have to rewrite the whole file.
there are several other checks such as if the UFL isn't constrained to 1.1 or of similar text length you can check for the delimitation position after UFL and then swap out text between ufl= and the deliminator.
Cyros
on 9 Jul 2014
Joseph Cheng
on 9 Jul 2014
Well it all depends on the file structure. I'm not sure if Aesop's fables are public domain or appropriate but you can substitute it with any text file. Even yours! The fable was just my default text file of choice for testing text files (especially if i don't have a representative text file to start working on yet). Why not change my ANT to UFL to ufl or play around with it and change the index locations for the data?
Cyros
on 10 Jul 2014
Categories
Find more on Data Import and Export in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!