How can I edit a value in multiple text files?

조회 수: 6 (최근 30일)
cigdemucf
cigdemucf 2019년 10월 24일
댓글: cigdemucf 2019년 10월 28일
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!
  댓글 수: 2
Bob Thompson
Bob Thompson 2019년 10월 24일
How much do you have written so far? How are you loading your files?
cigdemucf
cigdemucf 2019년 10월 24일
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));
fclose(fid);
% 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!

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

답변 (2개)

Zhangxi Feng
Zhangxi Feng 2019년 10월 24일
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
  댓글 수: 3
Zhangxi Feng
Zhangxi Feng 2019년 10월 24일
편집: Zhangxi Feng 2019년 10월 24일
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)
fprintf(fid,[fileText{i},'\n']);
end
fclose(fid);
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.
cigdemucf
cigdemucf 2019년 10월 27일
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')';
Hm0=str2num(fileText{1}(13:end));
Low_Hm0=round((0.8*Hm0),2);
fileText{1} (13:end)= num2str(Low_Hm0);
fid = fopen(fl,'w');
for i = 1:length(fileText)
fprintf(fid,[fileText{i},'\n']);
end
fclose(fid);
end
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!

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


Akira Agata
Akira Agata 2019년 10월 25일
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);
writecell(C,writeFilePath,'FileType','text','Delimiter','\t');
end
  댓글 수: 3
Akira Agata
Akira Agata 2019년 10월 28일
readcell function is introduced in R2019a, so the code should be revised for R2018b or older version. Could you tell us your MATLAB version?
cigdemucf
cigdemucf 2019년 10월 28일
Oh I see, mine is R2016a.

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by