replace first match with a value,second match with a different value etc
Show older comments
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
Answers (3)
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 Comments
Cedric
on 22 Jan 2013
Ok, I modified my example accordingly. Also see Julian answer; if your replacements were strings directly, his answer would be the most efficient.
Cedric
on 22 Jan 2013
Re-edited after you posted the file.
Andrew
on 22 Jan 2013
Cedric
on 22 Jan 2013
If A is a vector, you would just replace wg_y with A actually. We can go on interacting through the forum or you can email your script to me (if you do so, send the text file as well).
Andrew
on 22 Jan 2013
Jan
on 22 Jan 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.
Jan
on 22 Jan 2013
Equivalent to regexprep:
ResultString = sprintf(strrep(OriginalString, 'EE1', '%1.0fmm'), Value)
1 Comment
Cedric
on 24 Jan 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
Julian
on 22 Jan 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
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!