replace only the first match

I have a txt with 2000 lines. i want to open the txt,scan,find EE1 and replace it(only the first EE1,not the second) then create a new txt and close...
this is something a found but it replace all the EE1 it found.I want to replace only the first match..
close all; clear; clc
% Set user specific paths and values
% ======================================================================
HFSS_EXE_PATH='C:\Program Files (x86)\Ansoft\HFSS12';
SCRIPT_PATH='c:\HFSS\scriptttt.vbs';
NEW_VBS_FILE='c:\HFSS\scrip.vbs';
% ======================================================================
cc=0;
for wg_y=6:1:8
cc=cc+1;
% Replace first occurrence of waveguide Y dimension
%vbs_str=file2str(SCRIPT_PATH);
fid1=fopen(SCRIPT_PATH,'r+');
vbs_str=fscanf(fid1,'%c');
old_str=('Array("NAME:XSize", "Value:=", "EE1"');
new_str=sprintf('Array("NAME:XSize", "Value:=", "%1.0fmm")',wg_y);
new_vbs_str=strrep(vbs_str,old_str,new_str);
% Create updated VB script file
fid=fopen(NEW_VBS_FILE,'w');
fprintf(fid,'%s',new_vbs_str);
fclose(fid);
end
Thank you...

 Respuesta aceptada

Jan
Jan el 21 de En. de 2013
Editada: Jan el 21 de En. de 2013

0 votos

index = strfind(vbs_str, old_str);
first = index(1);
vbs_str = [vbs_str(1:first - 1), new_str, vbs_str(first + length(old_str):end)];

5 comentarios

Andrew
Andrew el 21 de En. de 2013
Τhank you that you answered so quick!...Can you please add your answer to my previous code..I am a little comfused...
Andrew
Andrew el 22 de En. de 2013
ok i found how to do it!!!!!thank you very much!!!!respect!!!
Andrew
Andrew el 22 de En. de 2013
Editada: Andrew el 22 de En. de 2013
Jan Simon can you help me again? Now i change only the first EE1 with a value.If i want to change the first EE1 i found with a value from a matrix (100,1) the second EE1 i found with a different value from a matrix (100,1) and the third EE1 i found with a different value from a matrix (100,1) ,etc (i have 100 EE1),how can i do it?
Thank you....
prashanth u
prashanth u el 19 de Abr. de 2021
What is new_str at 3rd line@Jan?
Jan
Jan el 20 de Abr. de 2021
new_str is the variable from the code in the question.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Variables en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 21 de En. de 2013

Comentada:

Jan
el 20 de Abr. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by