필터 지우기
필터 지우기

Edit entries in textfile with fopen

조회 수: 10 (최근 30일)
Ben
Ben 2014년 11월 3일
댓글: Abdullah 2015년 5월 5일
I need help while editing a control file of LS-DYNA via Matlab. In the following picture the structure of my file is depcited.
I need to change the entry in row number 12 to a specific value. I usually generate my output files with:
fileID = fopen(filename,permission);
fprintf(fileID, '...');
fclose(fileID);
However in this specific case this wont work, because depending on the permission, matlab discardes existing content or just adds new content to the end of the file.
My question is: What is the best way to open the file and edit an entry in a specific location? Performance matters...
Thanks for your help!
Kind regards

채택된 답변

Orion
Orion 2014년 11월 3일
편집: Orion 2014년 11월 3일
Hi,
One way to to do this.
Read the whole contents of your file in one cell.
Modify the line you want (be careful to keep the syntax).
rewrite the textfile with the new cell.
% read the data
fid = fopen('Myfile.txt','r');
MyText = textscan(fid,'%s','delimiter','\n');
fclose(fid);
%reconcatenate in a cell
MyText = [MyText{:}];
% Modify the content : here, the 12th line, new string.
MyText{12} = '2.3';
% rewrite the file
fid = fopen('Myfile.txt','wt');
fprintf(fid,'%s\n',MyText{:});
fclose(fid);
You can't really just specify a line to modify.
  댓글 수: 2
Ben
Ben 2014년 11월 3일
Ok, thanks for your help! I understand that I need to read the file into a cell structure, edit it in matlab workspace and write a new file. Too bad this reduces the performance more than I was hoping it would. Maybe i will find a workaround...
Guillaume
Guillaume 2014년 11월 3일
편집: Guillaume 2014년 11월 3일
No, you don't need to read the whole file. Too bad you accepted the wong answer too quickly!

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

추가 답변 (1개)

Guillaume
Guillaume 2014년 11월 3일
You would have to open the file with the r+ permission, read the file with fgetl to get line 12 position and then write the value you want. That's assuming the new value takes up as much space as the old one. If not, you would have to read and rewrite the remainder of the file.
Note that ,as the documentation of fopen states, you must call fseek or frewind between read and write operations
fid = fopen(filename, 'r+'); %open in read/write mode
for l=1:11
fgetl(fid); %read line 1 to 11
end
%we're now at the beginning of line 12
%the following is optional and just ensure that replacement is same length as line 12
pos = ftell(fid); %get current position
l12 = fgetl(fid); %read line 12
assert(length(l12) == length(replacement))
fseek(fid, pos, 'bof'); %go back to the beginning of line 12
%if you don't do the previous check then just
fseek(fid, 0, 'cof'); %fseek to current position to make sure write succeeds.
fprintf(fid, replacement);
fclose(fid);
  댓글 수: 3
Guillaume
Guillaume 2014년 11월 3일
You can vote for my answer by clicking on the triangle. That'll give me a little bit of credit.
Abdullah
Abdullah 2015년 5월 5일
thanks Guillaume for helping out, keep up the good work. Your code helped me out for automating a once time consuming and human-error prone updating of my code files in DSGE programing. now I can update the specific parameter values in DYNARE files for optimization, obtaining results and IRFs.

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

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by