How to run multiple programs

Hi,
Anybody know a code to run (say) 3 programs one after another without having to give a command to run each program please?
What I want is something like the following
for i=1:3
test(i).m;
end
Thanks

 Accepted Answer

Just put in an .m file (script):
test1; %you don't even need the .m here
test2;
test3;
And your three programs will run consecutively, provided they are in the path or the current folder.

6 Comments

thanks. If I have more than 3 programs is there a for loop I can use to do it easily. I dont know how to write a loop to change 1,2,3 in test1, test2, test3.
Then you can use something like what Geoff proposed:
your_string = 'test';
for ii = 1:3
current_prog = [your_string num2str(ii)];
run(current_prog)
end
dav
dav on 23 Oct 2014
Edited: dav on 23 Oct 2014
thanks to both of u!!
Could u pls explain this error I got .. "okkoma is the name I gave to the program.
Thanks
Undefined function or variable 'your_string'.
Error in okkoma (line 3)
current_prog = [your_string num2str(ii)];
current_prog = ['okkoma' num2str(ii)];
dav
dav on 23 Oct 2014
Edited: dav on 23 Oct 2014
I am sorry may be it wasn't clear I named the following code "okkoma"
your_string = 'test';
for ii = 1:2
current_prog = [your_string num2str(ii)];
run(current_prog)
end
But the programs run by the code "okkoma" are named test1, test2...
Is that wrong?
When I did the proposed change I got
Error using run (line 55)
okkoma1 not found.
Error in okkoma (line 5)
run(current_prog)
Thanks much
dav - so your main script is named okkoma.m and you want to run the two (or more) scripts named test1,test,etc.
Then, the code within the okkoma.m should be what José-Luis proposed earlier
your_string = 'test';
for ii = 1:2
current_prog = [your_string num2str(ii)];
run(current_prog)
end
which will call run on the scripts test1, test2, etc.

Sign in to comment.

More Answers (1)

Dav - it seems that you could use the run command to do this. If test is a cell array of strings with each string corresponding to a "program" (script or function with no arguments), then try
test = {'whos','what'};
for k=1:length(test)
run(char(test{k}));
end
If you run the above, the first command (whos) will list all variables in the workspace, and the second command (what) will list the MATLAB files in the folder.

3 Comments

hello,
Thanks for the response. But test1, test2, and test3 are complete matlab codes i am using. I dont understand why you use length(test) in your code.
Test1, test2, test3 are 3 different matlab codes
dav - it wasn't clear from your question what the names of are your scripts were. So in the above example, I created an array of strings where each string corresponded to a script. In this case, I used the built-in MATLAB functions whos and what. length is used to determine how many programs/scripts should be run (since the length(test) is two), and then we call run on each element of test. This could easily have been set to
test = {'test1', 'test2', 'test3'};
Thanks a lot.

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Tags

Asked:

dav
on 23 Oct 2014

Commented:

dav
on 25 Oct 2014

Community Treasure Hunt

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

Start Hunting!