running an external program by changing input text file for every iteration in windows.

Hello, i want to run an external program in matlab by changing the value of a certain parameter in the input text file. I want to scan a parameter for a certain range e.g 0-1000 in steps of 10. i want my script to change input file for every iteration, call the external program, run it and save the output files. Now my problem is divided in 2 steps.
1) I am reading the input file using
fid = fopen('input.in','r+');
data = textscan(fid, '%s', 'Delimiter', '\n', 'CollectOutput', true)
fclose(fid);
now i find many options to replace strings or numbers in text file, but i want to change the following line in input file "S_xoff(1)= 0" to "S_xoff(1)=100" and "S_yoff(1)=0" to "S_yoff(1)=100". how would i tell matlab to increase the number by 100 following a certain string.
2) I call the external program to run in matlab inside a for loop, as i want to do this for many iterations. My code runs successfully for one iteration, and then it stops. The problem is that it does not loop for multiple iterations. Any help is appreciated.

4 Kommentare

Have you considered using sprintf to form the search and replacement strings (or char arrays)?
hi, thanks for your answer, from help what i understood is sprintf will format the data in an array according to given format, but what i wanted to do is replace 0 with numbers within a string. How do i form replacement strings with sprintf?
str_to_find=sprintf('S_xoff(1)= %d',0);
str_to_paste=sprintf('S_xoff(1)= %d',100);
How exactly you can use this depends a bit on your specific file, but strrep seems to make sense as one of the options.
hi, many thanks for this. Both sprintf and strrep seems to work fine, I can see in my command window the string being replaced but they are not being saved in my text file. How would i ask matlab to save the replacement string in the original text file and close the file. I am using the following lines of code
% ------ open files and read the data in an array --------------
clc; clear;
fid = fopen ( 'input1.in' , 'r +' );
data = textscan (fid, '% s' , 'Delimiter' , '\ n' , 'CollectOutput' , true)
fclose (fid);
% -------- writing the text file and replacing text --------
fid = fopen ( 'input1.in' , 'w' );
for I = 1: length (data {1})
str = 'S_xoff (1) = 0' ;
newStr = strrep (str, '0' , '100e-6' ) % another option
end
fclose (fid);

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Rik
Rik am 25 Aug. 2020
Bearbeitet: Rik am 25 Aug. 2020
This should do the trick for you:
% ------ open files and read the data in an array --------------
clc; clear;
fid = fopen('input.in','r+');
data = textscan(fid, '%s', 'Delimiter', '\n', 'CollectOutput', true)
fclose(fid);
data=data{1};
% -------- replace text --------
str_to_find=sprintf('S_xoff(1)=%d',0);
str_to_paste=sprintf('S_xoff(1)=%d',100);
data=cellfun(@(x)strrep(x,str_to_find,str_to_paste),data,'UniformOutput',false);
% -------- writing the new text file --------
fid = fopen('input1.in','w');
for n = 1:numel(data)
fprintf(fid,'%s\n',data{n})
end
fclose (fid);

9 Kommentare

Hi, Many thanks for this code, but it does save the change in the input1.in. Input1.in is exactly the same as input.in. I attached the input file for your perusal. (I change the extension to .txt, since it was not attaching as .in file)
That is because there was a space after the equals sign that shouldn't have been there. I'll edit my answer.
data=readfile('https://www.mathworks.com/matlabcentral/answers/uploaded_files/351335/input.txt');
%(you can get readfile from the FEX or the AddOn manager, I'm using it here to read the file without downloading it)
str_to_find=sprintf('S_xoff(1)=%d',0);
str_to_paste=sprintf('S_xoff(1)=%d',100);
data=cellfun(@(x)strrep(x,str_to_find,str_to_paste),data,'UniformOutput',false);
Alternatively, you could also replace the entire line if you automatically determine which one it is.
true, thanks for pointing this out. This works perfectly now. i modified the code slightly generating the file name using sprintf while writing the new text file, using following lines
filename=sprintf('%s_%d.in','input',1)
fid = fopen(filename,'w');
for n = 1:numel(data)
fprintf(fid,'%s\n',data{n})
end
fclose (fid);
This also works fine, no problem. Now i took the code yto next step i-e running the newly generated text file to run an external program. This works well if i input directly the file name using
proc = System.Diagnostics.Process;
proc.Start('program.exe','input1.in');
but if i want to use the name generated using sprintf as in the following lines of code (instead of using above lines)
proc = System.Diagnostics.Process;
proc.Start('program.exe','filename.in');
this gives me error as filename.in not recognized. Is there some particular way to call the generated names using sprintf? i want to do like this because i want to apply a loop over the code and run e.g 1000 times changing values by 100 and generating output files everytime.
You are entering the filename as a char array, not as a variable:
proc = System.Diagnostics.Process;
proc.Start('program.exe',filename);
If you feel my answer solved your question, please mark it as accepted answer. If not, feel free to comment with any remaining issue.
Many thanks, your answer solves perfectly my first part of question.Now i extended the code using for loop to run it lets say 100 times, changing values from (100:10:1000), incrementing the change in s_xoff, creating new file, running it externally. The file name is also being incremented. I have the following code. This works well for first run of the loop, run the external program, but then stops, it does not do the next iteration of the loop. I am not sure what is stopping it to run the for loop. I am using the following code. (Please note that i have added an additional line to change in the input file).
clc; clear;
for i=0.0001:10:0.001
fid = fopen('input.in','r+');
data = textscan(fid, '%s', 'Delimiter', '\n', 'CollectOutput', true)
fclose(fid);
data=data{1};
% -------- replace text --------
str_to_find_x=sprintf('S_xoff(1)=%d',0);
str_to_paste_x=sprintf('S_xoff(1)=%d',i);
str_to_find_y=sprintf('S_yoff(1)=%d',0);
str_to_paste_y=sprintf('S_yoff(1)=%d',i);
data=cellfun(@(x)strrep(x,str_to_find_x,str_to_paste_x),data,'UniformOutput',false);
data=cellfun(@(y)strrep(y,str_to_find_y,str_to_paste_y),data,'UniformOutput',false);
% -------- writing the new text file --------
filename=sprintf('%s_%d.in','input',i)
fid = fopen(filename,'w');
for n = 1:numel(data)
fprintf(fid,'%s\n',data{n})
end
fclose (fid);
%--------run file----------
proc = System.Diagnostics.Process;
proc.Start('executable',filename); % Start the process
end
First an important piece of advice: always move code out of the loop if it doesn't depend in some way on the iteration:
clc; clear;
fid = fopen('input.in','r+');
data = textscan(fid, '%s', 'Delimiter', '\n', 'CollectOutput', true)
fclose(fid);
data=data{1};
str_to_find_x=sprintf('S_xoff(1)=%d',0);
str_to_find_y=sprintf('S_yoff(1)=%d',0);
for i=0.0001:10:0.001
% -------- replace text --------
str_to_paste_x=sprintf('S_xoff(1)=%d',i);
str_to_paste_y=sprintf('S_yoff(1)=%d',i);
data=cellfun(@(x)strrep(x,str_to_find_x,str_to_paste_x),data,'UniformOutput',false);
data=cellfun(@(y)strrep(y,str_to_find_y,str_to_paste_y),data,'UniformOutput',false);
% -------- writing the new text file --------
filename=sprintf('%s_%d.in','input',i);
fid = fopen(filename,'w');
%also possible instead of this loop:
%fprintf(fid,'%s\n',data{:});
for n = 1:numel(data)
fprintf(fid,'%s\n',data{n});
end
fclose(fid);
%--------run file----------
proc = System.Diagnostics.Process;
proc.Start('executable',filename); % Start the process
end
Apart from the point that you know which cell you should be changing and therefore could avoid the cellfun calls, there is a more pressing matter. Why did you define i that way? The rest of the code assumes it is an integer (because it uses %d in the calls to sprintf), and because you have a step size that is so large you will only have a single iteration. Why didn't you do 100:10:1000 if you wanted that?
You could have caught this problem yourself if you had put a break point at the first line of code and stepped through your code line by line.
many thanks for pointing it out, this is my mistake, should have been "i=0.0001:0.1:0.001" instead of "i=0.0001:10:0.001". I wanted to have this range "0.0001:0.1:0.001" in my iterations to balance the units. Now I have balanced the units in the input file and ran the code using range 100:10:100. now it works over the loop. many thanks for pointing this out
In the future you can also run your loop definition separately:
i=0.0001:0.1:0.001;
%returns i=0.0001
So that is probably still not what you meant. But good your code is correctly now.
If you want to change the value to a non-integer: read the documentation of sprintf for your options (the format specification works the same for fprintf and mostly the same for num2str).
Many thanks for your guidance.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Gefragt:

am 24 Aug. 2020

Kommentiert:

am 26 Aug. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by