function for editing a file

1 view (last 30 days)
Joseph Stalin
Joseph Stalin on 5 Mar 2013
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

Accepted Answer

Jan
Jan on 5 Mar 2013
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 Comment
Joseph Stalin
Joseph Stalin on 8 Mar 2013
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?

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink Coder in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!