Different behaviour while running Matlab script from DOS window
2 次查看(过去 30 天)
显示 更早的评论
Hi,
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.
1 个评论
Walter Roberson
2016-9-18
Note: there is an R2010bSp2 that you are entitled to if you have R2010bSp1 .
采纳的回答
Walter Roberson
2016-9-18
In the R2010b timeframe, the command
add.m
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.
3 个评论
Walter Roberson
2016-9-19
You are invoking MATLAB with -r "add.m"
Anything that you include in the -r option is eval()'d as a string, so it is the same as if you had typed it at the command line.
You should be considering using -r "run add.m" or -r "add"
更多回答(1 个)
Jan
2016-9-16
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.
4 个评论
Nagaraj Ramachandra
2016-9-19
The issue mentioned by me is regarding running M-script from Windows Command Prompt.
In my case, I cannot add the script folder to MATLAB's search path. I have lots of M-scripts(which are in different locations) which I want to run from Windows command prompt.
I have created a service request and Mathworks engineer is looking into the issue.
Jan
2016-9-20
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')"
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!