Different behaviour while running Matlab script from DOS window
2 views (last 30 days)
Show older comments
Nagaraj on 16 Sep 2016
Commented: Jan on 20 Sep 2016
I have a problem starting Matlab from DOS window for different versions of Matlab.
When I call Matlab 2010bSp1 (32 bit) from DOS window to run a script, I do not see any problem.
C:\ProgramFiles\matlab\r2010bsp1\bin\matlab.exe -r "add.m"
When I call Matlab 2014a (64 bit) from DOS window to run the same script, I see a problem
C:\ProgramFiles\matlab\r2014a\bin\matlab.exe -r "add.m"
The MATLAB command window shows Undefined variable "add" or function "add.m".
Note that the path of "add.m" is not added to search path of Matlab in either case.
Can anyone please let me know why MATLAB r2014a version is not able to identify "add.m"?
Any help is highly appreciated.
Note: there is an R2010bSp2 that you are entitled to if you have R2010bSp1 .
In the R2010b timeframe, the command
would generate the error message
??? Undefined function or variable 'add'.
In R2014a the same basic message is
Undefined variable "add" or function "add.m".
By R2016a the message had become
Undefined variable "add" or class "add.m".
The difference between R2010b and R2014a reflects an underlying change in processing. In R2010b, if the function add did exist in scope, then when the syntax add.m was used, add would be invoked with no arguments, and if that did not lead to an error message itself, MATLAB would then fail trying to access the field m of the function return value (even if a structure with that field was returned) because it is not permitted in MATLAB to subsref the result of a function call. But by the time of R2014a, MATLAB's parser had been extended to examine the call as a whole and reject it as being invalid without having invoked the function add in that situation.
This does lead to a difference: in the R2010b timeframe, if the function add existed and contained a quit() or exit() call that was executed, then MATLAB would exit before it noticed that taking a structure reference off of a function return value was invalid. But in later versions, because the function would not be invoked at all in that situation, you would end up with the error message.
So, long story short: do not include the '.m' in a function call. You can include it in a run action, but not in a function call.
More Answers (1)
Jan on 16 Sep 2016
If add.m is not added to the Matlab path, it should not work in R2010. Why does this Matlab version find the function? Is it in the userpath or in the current path?
If you want Matlab 2014a to find the function, adding it to the Matlab path is obligatory.
Jan on 20 Sep 2016
You can add the path easily when Matlab is called from the command line:
matlab -r "addpath('C:\Your\Path'); add"
or if add.m is a script and not a function file:
matlab -r "run('C:\Your\Path\add')"
Find more on Programming Utilities 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!