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

3 ビュー (過去 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!
  1 件のコメント
Jude
Jude 2024 年 7 月 31 日
I appreciate the help friend, I suspected it was something like that but I did not realize you could put quotes inside quotes like that. Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming Utilities についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by