Borrar filtros
Borrar filtros

function for editing a file

1 visualización (últimos 30 días)
Joseph Stalin
Joseph Stalin el 5 de Mzo. de 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

Respuesta aceptada

Jan
Jan el 5 de Mzo. de 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 comentario
Joseph Stalin
Joseph Stalin el 8 de Mzo. de 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?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Simulink Coder en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by