MATLAB Answers


How can I edit a value in multiple text files?

Asked by cigdemucf on 24 Oct 2019
Latest activity Commented on by cigdemucf on 28 Oct 2019
I need to reduce the wave height (Hm0) values by 20% in 91 JONSWAP files that are named jonswap_#.inp. I attached two of them. I would appreciate your help with the code, thank you!


How much do you have written so far? How are you loading your files?
I wrote the following code which reads and edits the target value, and writes it back into the text file but it removes the rest of the lines.
% Read the first line into tline
fid = fopen('jonswap_1.inp','r');
tline = fgetl(fid);
% Extract value of Hm0 from tline
Hm0= str2num(tline(13:16));
% Reduce Hm0 by 20% and write it back into tline
Low_Hm0= 0.8*(Hm0);
tline(13:17)= num2str(Low_Hm0);
% Write tline back into the 1st line of the text file
fid = fopen('jonswap_1.inp', 'w');
fprintf(fid,'%s', tline); %this writes it back to the text file
% but removes rest of the lines (fp, mainang etc)
I have not gotten to the point where I put this in a loop to run this for the other jonswap files (jonswap_1.inp to jonswap_91.inp)
Thank you in advance!

Sign in to comment.




2 Answers

Zhangxi Feng 님의 답변 24 Oct 2019

A simple way to do this is to simply write the entire file, I assume it is not a large file.
You can do something like this:
function write(fname,input)
fileID = fopen(fname,'w');
fprintf(fileID,'Hm0\t= %5.1f\n',input(1));
fprintf(fileID,'fp\t= %5.1f\n',input(2));
You get the idea


Rewriting the file would be fine if it was only one but there are 91 files with varying input. Is there a way that I can only edit the first line of the text file?
You mean the 91 files have different parameters with different numbers?
You can read in the file as a whole, change the first line, then write the whole file back out.
Something like:
file = fileread('New.inp');
fileText = regexp(file, '\r\n|\r|\n', 'split')';
fileText{1}(15:end) = num2str(0.98);
fid = fopen('New.inp','w');
for i = 1:length(fileText)
Note regexp will have issues if you try to use this across platforms. It works fine on Windows but may have issues on Linux. You can always read the file in using fgetl, I use regexp for less coding.
It mostly worked, thank you.
I edited to automate reducing the Hm0 by 20% instead of assigning 0.98 to it as follows;
and I put it in a loop for so it changes all 91 files.
for j=1:91
fl=strcat('jonswap_', num2str(j), '.inp')
file = fileread(fl);
fileText = regexp(file, '\r\n|\r|\n', 'split')';
fileText{1} (13:end)= num2str(Low_Hm0);
fid = fopen(fl,'w');
for i = 1:length(fileText)
But the problem here is, Hm0 is not always in the same precision as the Low_Hm0, so I get the following error
fileText{1} (13:end)= num2str(Low_Hm0);
In an assignment A(:) = B, the number of elements in A and B must be the same.
I tried rounding Low_Hm0 but it still has zeros after decimal point (eg: 1.0000 or 0.8000) I tried setting up digit count to 4 with digit(4) so it has just two digits after decimal points (eg:1.00 or 0.80) but then I got the following error. (I am using the university's Matlab licence)
digits (4)
License checkout failed.
License Manager Error -87
Checkout exceeds MAX specified in options file.
Contact your License Administrator to review the Options File.
Any idea how to proceed from here? Thank you!

Sign in to comment.

Akira Agata 님의 답변 25 Oct 2019

I believe it's better to keep the original files and save the revised files to a different folder.
How about the following?
In this code, original .inp files are assumed to be stored in \Data1 folder, and revised .inp files will be saved to \Data2 folder.
fileList = dir(fullfile(pwd,'Data1','*.inp'));
for kk = 1:numel(fileList)
readFilePath = fullfile(fileList(kk).folder, fileList(kk).name);
C = readcell(readFilePath,'FileType','text');
C{1,3} = C{1,3}*0.8;
writeFilePath = fullfile(pwd,'Data2',fileList(kk).name);


I am not sure if some of the functions you defined (such as readcell) are a part of a toolbox that I don't have but I couldn't get this script run. filelist also did not priduce folder or filetype fields to be read in this line readFilePath = fullfile(fileList(kk).folder, fileList(kk).name)
readcell function is introduced in R2019a, so the code should be revised for R2018b or older version. Could you tell us your MATLAB version?
Oh I see, mine is R2016a.

Sign in to comment.