How to execute multiple m files in current project from command line in windows?

Hi,
I have got a large simulink project an multiple m files to run in this project. At the moment I start to first m file which loads the project with the following command
matlab -r "run('matlabfile1.m')"
in windows command line. When matlabfile1 finished the file closes itself but not the simulink project. In the next step I would like to run the second (and more) m file in the same project.
I have the following ideas:
  • run a matlab file automatically when opening it. Perhaps there exists a way to implement that
  • run the next matlab file in a new matlab instance and communicate between the matlab instances so that the file is able to run the simulink project
  • run a seperate matlab file in the background which is watching for commands (perhaps from windows command line) to run different matlab files
Are there some ideas to solve my problem?

3 Comments

What does "the file closes itself" mean? Which file closes what?
What does the "it" mean in "run a matlab file automatically when opening it"?
If you start a new Matlab instance for each M-file and "communicate" with a formerly existing Matlab session, you will end at N open Matlab sessions for N m-files. This will exhaust your memory soon.
I am using the following commands to close the m file at the and of the file
edtSvc = com.mathworks.mlservices.MLEditorServices ;
edtSvc.getEditorApplication.closeNoPrompt ;
Your second question: I'd like to open a file and run this file automatically.
When opening multiple matlab sessions I would close each session before opening a new own with
exit
Why do you open the editor at all if you want to run the code?
exit is equivalent to quit.
I'm still not sure about what the problem is. Do you want to start a bunch of scripts one after the other without closing Matlab?

Sign in to comment.

 Accepted Answer

What about running this in the Windows command line:
matlab -r "for k=1:21, run(sprintf('matlabfile%d.m', k)); end"
Now the scripts matlabfile1.m to matlabfile21.m run one after the other in one Matlab session.
Or maybe you mean:
matlab -r "run('matlabfile1.m'); quit"
This closes the Matlab session after calling the script. In opposite to the first suggestion, this wastes a lot of time with opening new Matlab sessions.
I'd prefer to solve this in Matlab directly: Why not creating an M-file, which calls the wanted scripts? This is not a big difference to starting the for loop in -r argument. But then the work is controlled completely in Matlab and not in a mixture of Matlab and the command line of the operating system.

2 Comments

The files don't have got the names matlabfile1, matlabfile2, etc. I just named it like that for my question.
they are named: matlab_build.m, matlab_performance.m, matlab_track.m etc.
@Sandra: Please post the details in the original question already. This is more efficient than letting the readers guess. What exactly is "etc."?
Would this solve the problem:
matlab -r "run('matlab_build'); run('matlab_performance'); run('matlab_track'); quit"
Or is it smarter to create an M-file, which does, what you want?
function myListOfScripts
matlab_build;
matlab_performance;
matlab_track;
end
and start this by
matlab -r "myListOfScripts; exit"
If this does not do, what you want, please try again to explain, what you want to achieve.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2016a

Asked:

on 14 Dec 2018

Edited:

Jan
on 14 Dec 2018

Community Treasure Hunt

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

Start Hunting!