How can I have a script open a new MATLAB instance and run a different script in that new instance?
7 views (last 30 days)
Show older comments
The title pretty much says it, I am trying to write a scirpt file that will, upon execution, open a new instance of MATLAB and have that new instance run a different script. I currently have the following:
system('"C:\Program Files\MATLAB\R2024a\bin\matlab.exe" -r "run("SecondScript.m")"')
I based this on the answer to a somewhat similar forum post I found, but have not been able to get it to work properly. Executing this line successfully opens the new MATLAB instance and seems to try running SecondScript.m, but throws an Unable to resolve the name 'SecondScript.m' error in the new instance. I'm fairly unfamiliar with the use of the system function and the -r flag, so any help would be appreciated.
I'll also add that I am aware that the Parellel Computing Toolbox can do something similar, however that is not an option for me.
1 Comment
Walter Roberson
on 30 Jul 2024
Edited: Walter Roberson
on 30 Jul 2024
system('"C:\Program Files\MATLAB\R2024a\bin\matlab.exe" -r "run(''SecondScript.m'')"')
This leaves open the question of which directory MATLAB is going to start in. It is probably more robust to name the full path to the script file.
Accepted Answer
Avni Agrawal
on 30 Jul 2024
Edited: Avni Agrawal
on 30 Jul 2024
I understand that you are trying to write a script file that will, upon execution, open a new instance of MATLAB.It looks like there might be an issue with the way the script path is being passed to the MATLAB command. When using the `-r` flag, MATLAB expects the command to be a valid MATLAB command, and it seems that the quotes around `SecondScript.m` might be causing issues.
Here is a revised version of your command that should work:
system('"C:\Program Files\MATLAB\R2024a\bin\matlab.exe" -r "run(''SecondScript.m'');"')
MATLAB uses single quotes to denote strings, so you need to escape the single quotes around `SecondScript.m` by using double single quotes (`''`).
If `SecondScript.m` is not in the current working directory of the new MATLAB instance, you might need to provide the full path to the script. For example:
system('"C:\Program Files\MATLAB\R2024a\bin\matlab.exe" -r "run(''C:\\path\\to\\SecondScript.m'');"')
Notice the double backslashes (`\\`) in the path to escape the backslash character. Replace `C:\\path\\to\\SecondScript.m` with the actual path to your `SecondScript.m` file.
I hope this helps!
More Answers (0)
See Also
Categories
Find more on Programming Utilities 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!