Clear Filters
Clear Filters

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

29 views (last 30 days)
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 Comments
Adam Hicks
Adam Hicks on 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 on 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.

Sign in to comment.

Accepted Answer

Adam Hicks
Adam Hicks on 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 Comments
ME
ME on 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 on 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), ')"'])

Sign in to comment.

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!