function for editing a file

조회 수: 2 (최근 30일)
Joseph Stalin
Joseph Stalin 2013년 3월 5일
Dear Techies, I want to read and write a file with some changes. I am able to read a particular line using fgetl().After reading the line I want to edit the line and save it in the same file. What function I need to use for this purpose?
reg, joseph

채택된 답변

Jan
Jan 2013년 3월 5일
This possible by using fwrite or fprintf when the number of characters do not change. If it does or could change, there is no other way than reading the complete file, modifying the data and re-write it:
Str = fileread(FileName);
Str = strrep(Str, char([13, 10]), char(10)); % Care for linebreaks
CStr = regexp(Str, char(10), 'split');
CStr{23} = strrep(CStr{23}, 'short string', 'much longer string');
FID = fopen(FileName, 'w'); % or wt
if FID == -1, error('Cannot open file for writing: %s', FileName); end
fprintf(FID, '%s\n', CStr{:});
fclose(FID);
  댓글 수: 1
Joseph Stalin
Joseph Stalin 2013년 3월 8일
Thanks Jan.I have one more question. In scripting we are using these braces for accessing matrices.{},() I just want to know which braces to use when?

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by