Effacer les filtres
Effacer les filtres

How to run a .m file in a new MATLAB instance?

29 vues (au cours des 30 derniers jours)
Adam Hicks
Adam Hicks le 13 Nov 2017
Commenté : Stephen23 le 29 Nov 2023
I'm running a script which calls several functions, and one of these only works in a new version of MATLAB (R2016b). I'm trying to get my version of MATLAB (R2010b) to open a new instance of 2016b MATLAB and run a script within it to finish my program. I currently have:
!"C:\Program Files\MATLAB\R2016b\bin\matlab.exe" NewFunction.m
which opens a new instance of MATLAB R2016b but does not execute NewFunction.m within it (or at all).
  2 commentaires
Adam Hicks
Adam Hicks le 13 Nov 2017
!"C:\Program Files\MATLAB\R2016b\bin\matlab.exe" -r NewFunction.m &
This tells the new MATLAB instance to run NewFunction using CLI.
However, it seems that it is trying to run NewFunction.m before MATLAB fully starts up and says, "Undefined variable "NewFunction" or class "NewFunction.m"".
Is there a way I can tell MATLAB to wait before executing the command? As far as I know this functionality doesn't exist in Unix/CLI.
Steven Lord
Steven Lord le 13 Nov 2017
When you use the -r flag, whatever is after it should be a valid MATLAB command. "NewFunction.m" is not, unless m is a MATLAB program file inside a package named NewFunction or is a Static method of a class named NewFunction.
If you want to execute a MATLAB program file named NewFunction.m you would need "path_to\matlab.exe -r NewFunction" since NewFunction is what you would type in the MATLAB Command Window to execute that program file.

Connectez-vous pour commenter.

Réponse acceptée

Adam Hicks
Adam Hicks le 13 Nov 2017
I found the solution here .
!"C:\Program Files\MATLAB\R2016b\bin\matlab.exe" -r "NewFunction"
This will run NewFunction.m on startup in the new instance of MATLAB.
  5 commentaires
ME
ME le 29 Nov 2023
this handles inputArg1 as a string... you could try buidling this command via eval and strcat:
inputArg1 = 1;
eval(['!"C:\Program Files\MATLAB\R2016b\bin\matlab.exe" -r "NewFunction(' num2str(inputArg1) ')"'])
Stephen23
Stephen23 le 29 Nov 2023
"you could try buidling this command via eval and strcat:"
Do NOT do that. Call the SYSTEM function. For example:
system(['"C:\Program Files\MATLAB\R2016b\bin\matlab.exe" -r "NewFunction(',num2str(inputArg1), ')"'])

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Environment and Settings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by