Different behaviour while running Matlab script from DOS window

2 vues (au cours des 30 derniers jours)
Nagaraj
Nagaraj le 16 Sep 2016
Commenté : Jan le 20 Sep 2016
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 commentaire
Walter Roberson
Walter Roberson le 18 Sep 2016
Note: there is an R2010bSp2 that you are entitled to if you have R2010bSp1 .

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 18 Sep 2016
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 commentaires
Walter Roberson
Walter Roberson le 19 Sep 2016
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 le 20 Sep 2016
Great! It works now. Thanks a lot.

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 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.
  4 commentaires
Nagaraj Ramachandra
Nagaraj Ramachandra le 19 Sep 2016
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 le 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')"

Connectez-vous pour commenter.

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by