replace first match with a value,second match with a different value etc

1 visualización (últimos 30 días)
I have a txt with 2000 lines.In this txt EE1 appears 100 times.If i want to change the first EE1 i found with a value from a matrix (100,1) the second EE1 i found with the second value from the same matrix (100,1) and the third EE1 i found with the third value from the same matrix (100,1) ,etc ,how can i do it?
This follow code is a mix from something i found and the help of Jan Simon. It replaces only the first EE1 in the txt.
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'; % ======================================================================
for wg_y=6:1:8
% Replace first occurrence of ΕΕ1
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);
index = strfind(vbs_str, old_str);
new_vbs_str = [vbs_str(1:index(1) - 1), new_str, vbs_str(index(1) + length(old_str):end)];
% Create updated VB script file
fid=fopen(NEW_VBS_FILE,'w'); fprintf(fid,'%s',new_vbs_str); fclose(fid);
end
  2 comentarios
Andrew
Andrew el 22 de En. de 2013
the txt is like
"Nominal"), "M44m:=", Array("Nominal"), "M44p:=", Array("Nominal"), "N1p:=", Array( _
"Nominal"), "N2p:=", Array("Nominal"), "N2m:=", Array("Nominal"), "N3p:=", Array( _
"Nominal"), "N3m:=", Array("Nominal"), "N4p:=", Array("Nominal"), "O1m:=", Array( _
"Nominal"), "O3p:=", Array("Nominal"), "O4m:=", Array("Nominal"), "O4p:=", Array( _
"Nominal")), Array("X Component:=", "Freq", "Y Component:=", Array( _
"dB(PeakGain)")), Array()
oModule.ExportImageToFile "XY Plot 2", "C:/HFSS/scripts/3001.jpg", 0, 0
oModule.ExportToFile "XY Plot 2", "C:/HFSS/scripts/4001.txt"
oProject.Save
oEditor.ChangeProperty Array("NAME:AllTabs", Array("NAME:Geometry3DCmdTab", Array("NAME:PropServers", _ "E1:CreateBox:1"), Array("NAME:ChangedProps", Array("NAME:XSize", "Value:=", "EE1")))) oProject.Save oDesign.AnalyzeAll Set oModule = oDesign.GetModule("ReportSetup") oModule.CreateReport "XY Plot 1", "Terminal Solution Data", "Rectangular Plot", _ "Setup1 : Sweep1", Array("Domain:=", "Sweep"), Array("Freq:=", Array("All"), "A2m:=", Array( _ "Nominal"), "A2p:=", Array("Nominal"), "A4p:=", Array("Nominal"), "A3m:=", Array( _ "Nominal"), "A3p:=", Array("Nominal"), "B3m:=", Array("Nominal"), "B2m:=", Array( _ "Nominal"), "C3m:=", Array("Nominal"), "C3p:=", Array("Nominal"), "C2m:=", Array( _ "Nominal"), "C2p:=", Array("Nominal"), "C1p:=", Array("Nominal"), "C4p:=", Array( _ "Nominal"), "D1m:=", Array("Nominal"), "D2p:=", Array("Nominal"), "E1p:=", Array( _<<http://mathworks.com/matlabcentral/images/surf.gif>>
Jan
Jan el 22 de En. de 2013
@Andrew: The format of your text is not clear.

Iniciar sesión para comentar.

Respuestas (3)

Cedric
Cedric el 22 de En. de 2013
Editada: Cedric el 22 de En. de 2013
Here is a funny one-liner for you, that could be adapted to your needs:
>> str = 'abc EE1 d EE1 00EE100' ; % Dummy example.
>> num = [10, 20, 34] ;
>> sprintf(regexprep(str, 'EE1', '%1.0fmm'), num)
ans = abc 10mm d 20mm 0034mm00
.. but you would have to tell us a bit more about the nature of your "numbers". Is this format '%1.0f' the one that you need, and do you also need to add 'mm' ?
EDIT: just to be clear, if the 100 occurrences of EE1 belong to the same file and your 100 numeric values are stored in a vector called wg_y, your code should be something like that:
fid = fopen(SCRIPT_PATH, 'r+') ;
vbs_str = fscanf(fid, '%c') ;
fclose(fid) ;
new_vbs_str = sprintf(regexprep(vbs_str, 'EE1', '%1.0fmm'), wg_y) ;
fid = fopen(NEW_VBS_FILE, 'w') ;
fprintf(fid, '%s', new_vbs_str) ;
fclose(fid) ;
Cheers,
Cedric
  7 comentarios
Jan
Jan el 22 de En. de 2013
@Andrew: You have asked before you sent data by mail. I really appreciate this. Some other users think, that their problem is more important than the spare time of the ones they want a solution from.

Iniciar sesión para comentar.


Jan
Jan el 22 de En. de 2013
Equivalent to regexprep:
ResultString = sprintf(strrep(OriginalString, 'EE1', '%1.0fmm'), Value)
  1 comentario
Cedric
Cedric el 24 de En. de 2013
Actually, as I had never really used strrep (I might be too regexp-addicted for that ;-)), I just looked at the doc. now that I read your answer, and saw that strrep has an interesting behavior with overlapping occurrences of the pattern. Certainly something that I should keep in mind!
>> strrep( 'AAA', 'AA', 'Hello' )
ans = HelloHello
>> regexprep( 'AAA', 'AA', 'Hello' )
ans = HelloA

Iniciar sesión para comentar.


Julian
Julian el 22 de En. de 2013
Did you consider using regular expressions, e.g.
regexprep('some text ee1 some more text ee1 ee1', 'ee1', {'FIRST' 'SECOND' 'THIRD'}, 'once')
ans =
some text FIRST some more text SECOND THIRD
  3 comentarios
Cedric
Cedric el 22 de En. de 2013
I know what it is, it happens to me all the time, especially when I open multiple threads all at once in multiple tabs ;-)
Andrew
Andrew el 22 de En. de 2013
EE1 is not in the same line ...i just added a part of the txt

Iniciar sesión para comentar.

Categorías

Más información sobre Text Data Preparation 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