Filter löschen
Filter löschen

function for editing a file

1 Ansicht (letzte 30 Tage)
Joseph Stalin
Joseph Stalin am 5 Mär. 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

Akzeptierte Antwort

Jan
Jan am 5 Mär. 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 Kommentar
Joseph Stalin
Joseph Stalin am 8 Mär. 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?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Simulink Coder finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by