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

29 次查看(过去 30 天)
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 个评论
Adam Hicks
Adam Hicks 2017-11-13
!"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 2017-11-13
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.

请先登录,再进行评论。

采纳的回答

Adam Hicks
Adam Hicks 2017-11-13
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 个评论
ME
ME 2023-11-29
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 2023-11-29
"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), ')"'])

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by