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

7 visualizaciones (últimos 30 días)
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 comentario
Walter Roberson
Walter Roberson el 30 de Jul. de 2024
Editada: Walter Roberson el 30 de Jul. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Avni Agrawal
Avni Agrawal el 30 de Jul. de 2024
Editada: Avni Agrawal el 30 de Jul. de 2024
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 comentario
Jude
Jude el 31 de Jul. de 2024
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!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming Utilities en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by