Different behaviour while running Matlab script from DOS window

조회 수: 2 (최근 30일)
Nagaraj
Nagaraj 2016년 9월 16일
댓글: Jan 2016년 9월 20일
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
Walter Roberson 2016년 9월 18일
Note: there is an R2010bSp2 that you are entitled to if you have R2010bSp1 .

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
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
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"
Nagaraj Ramachandra
Nagaraj Ramachandra 2016년 9월 20일
Great! It works now. Thanks a lot.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
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
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
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 CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by