how to editing the value of a parameter in the textfile

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

Is there just one line in this file or multiple lines? Given the commas, is this a csv file of some sort?
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.
i open the file with fopen(), then read it in with strfind(). how do i rewrite the part after "ufl="? then fclose()
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
I've removed the duplicate question. Please post a question only once.

Sign in to comment.

Answers (1)

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

thans Joseph, i'll test this tomorrow. regards Cyros
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.
i created a text file with "The Ant and the Grasshopper" and applied you script but it didnt work, i couldnt change Ant to ANT. however, im going to work on it, and maybe ill find something out.
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?
yes, thats what im actually doing, playing around to understand the code. i tested it on my file and i can change the value of UFL while debugging in the command window, but cant print it back to the file. im getting an errof for fseek and dont understand what for it is.

Sign in to comment.

Categories

Asked:

on 8 Jul 2014

Commented:

Jan
on 12 Jul 2014

Community Treasure Hunt

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

Start Hunting!