How can I have a script open a new MATLAB instance and run a different script in that new instance?

7 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2024-7-30
编辑:Walter Roberson 2024-7-30
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.

请先登录,再进行评论。

采纳的回答

Avni Agrawal
Avni Agrawal 2024-7-30
编辑:Avni Agrawal 2024-7-30
Hi @Jude,
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!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming Utilities 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by