- Reading 4th line and creating the changed version based on userInput1 and userInput2.
- Writing all the lines in a new file with changed 4th line.
Changing values(parameteres) in input text file at a certain line
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
RAKESH KUMAR TOTA
le 12 Mar 2020
Commenté : RAKESH KUMAR TOTA
le 16 Mar 2020
MATErial,1
SOLId
PLANe STRAin
ELAStic ISOTropic 1000.0 0.25
! Blank termination record
COORdinates
1 0 0.0 0.0
2 0 4.0 0.0
3 0 10.0 0.0
4 0 0.0 4.5
5 0 5.5 5.5
6 0 10.0 5.0
7 0 0.0 10.0
8 0 4.2 10.0
9 0 10.0 10.0
! Blank termination record
ELEMents
1 1 1 1 2 5 4
2 1 1 2 3 6 5............................
The text file looks like this. I want to change the fourth line last two parameters 1000.0 and 0.25 to user defined input parameters and run the system command to execute the input text file in external program. Any help could you greatly appreciated. Thanks in advance
0 commentaires
Réponse acceptée
Raunak Gupta
le 16 Mar 2020
Hi,
You can use textscan while reading specific line from the text file and in this case it’s the 4th line. Since there is some initial whitespaces in the 4th line which will go after reading from textscan I have added a tab character which compensates for same.
The following code may help you achieved the same.
userInput1 = 50; % User Input 1
userInput2 = 0.5; % User Input 2
% Original File
fid=fopen('check_nf.txt','r');
linenum = 4;
C = textscan(fid,'%s',1,'delimiter','\n', 'headerlines',linenum-1);
line_4_words = strsplit(string(C{1}));
line_4_words(3) = num2str(userInput1);
line_4_words(4) = num2str(userInput2);
changedLine = sprintf('\t') + join(line_4_words) + newline;
fclose(fid);
count = 1;
fid=fopen('check_nf.txt','r');
% New File
fod=fopen('new_nf.txt','w');
while ~feof(fid)
if count == 4
fprintf(fod,'%s',changedLine);
fgets(fid);
else
fprintf(fod,'%s',fgets(fid));
end
count = count + 1;
end
fclose(fod);
fclose(fid);
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Data Import and Export dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!