Borrar filtros
Borrar filtros

how edit and write into particular line consisting of letters and numbers text file using matlab?

1 visualización (últimos 30 días)
** BOUNDARY CONDITIONS
**
** Name: Surf_temp1 Type: Temperature
*Boundary
Surf_temp_Set-1, 11, 11, 100.
** Name: Surf_temp2 Type: Temperature
*Boundary
Surf_temp_Set-2, 11, 11, 100.
** Name: Surf_temp3 Type: Temperature
*Boundary
Surf_temp_Set-3, 11, 11, 100.
** Name: Surf_temp4 Type: Temperature
*Boundary
Surf_temp_Set-4, 11, 11, 500.
This is text file to be edited. How to search particular boundary condition and edit and write to new text file?..Suppose I want to change
*Boundary
Surf_temp_Set-4, 11, 11, 500.
as
*Boundary
Surf_temp_Set-4, 11, 11, 350.

Respuesta aceptada

KSSV
KSSV el 2 de Mzo. de 2017
f= fopen('your data in text file');
c = textscan(f,'%s','delimiter','\n');
fclose(f) ;
S = c{1} ;
%
str = {'Surf_temp_Set-4, 11, 11, 350.'} ; % to be replaced
str1 = strsplit(str{1}) ;
% get the index which is to be repalced
idx1 = find(not(cellfun('isempty',strfind(S, str1{1}))));
% Repalce
S(idx1) = str ;
% write to a rext file
fid = fopen('file.txt','w') ;
fprintf(fid,'%s\n',S{:});
fclose(fid) ;
  1 comentario
raghavendra g g
raghavendra g g el 2 de Mzo. de 2017
Thank you KSSV. In this line Surf_Set_4, 11, 11, 350.... the last number 350 need to be updated for every iterations like it changes to 400,500,600,700. how to do this?

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 2 de Mzo. de 2017
Unfortunately, the only way to update a text file in MATLAB is to read in the old file, make whatever changes you need, and write out a new file containing the new content.
It is safer to always write to a different file, not the input file, in case something goes wrong during the writing (bug, system fails, disk fills up.)
This inability to effectively update a text file in place is due to the way that text files have been represented in nearly all systems since about the time that room-sized computers started being replaced by smaller computers.

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by