error associated with Nargin

5 ビュー (過去 30 日間)
Yogi Thambiah
Yogi Thambiah 2018 年 1 月 24 日
コメント済み: Walter Roberson 2018 年 1 月 24 日
I download Matlab code and it worked well in an old version of Matlab. However, when I run it in Matlab R2017b I get an error message related to nargin. It says, you can only call nargin/narginout from within MatLab function.
  1 件のコメント
Image Analyst
Image Analyst 2018 年 1 月 24 日
OK. Do you have a question? You've included neither the error message nor the code, so what can we say?

サインインしてコメントする。

回答 (2 件)

Rik
Rik 2018 年 1 月 24 日
I just tested to confirm. For some odd reason you could use the nargin function in a script m-file. This returned the value for the calling function. The solution is to actually use the nargin function in a context it makes sense: a function.
The release notes also say this:
Move calls to nargin, nargout, and inputname into the relevant function file. If you need the results in a script, call the functions and store the results in a variable. Then use the variable in the script.

Walter Roberson
Walter Roberson 2018 年 1 月 24 日
This was a change as of R2016b. Before that, if you called nargin from inside a script it would not error but would return 0; as of R2016b it errors.
There was never a great reason to call nargin from inside a script; about the only use for it was that if you temporarily commented out a function line so that variables would not disappear when the function exited, then you did not have to change nargin references.
You should either add a function header to the code to convert it to a function (in which nargin is a meaningful thing to query), or else you should replace the nargin with 0 (and potentially clean up the code afterwards.)
  2 件のコメント
Rik
Rik 2018 年 1 月 24 日
When I tested it, I got seemingly random numbers. For example, calling a file with only disp(nargin) inside it results in 123863040 (win10x64, R2012b). In 6.5 it results in -2.1475e+009.
But to be fair, I called the file directly, not from inside another function.
Walter Roberson
Walter Roberson 2018 年 1 月 24 日
Hmmm, not much pattern on OS-X version
  • R2010bSP1 -> 1
  • R2011a -> 32767 (0x7FFF)
  • R2011b -> 32767 (0x7FFF)
  • R2012a -> -483851947 (0xE3290155)
  • R2012b -> 28672 (0x7000)
  • R2013a -> 28672 (0x7000)
  • R2013b -> 0
  • R2014a -> 32711 (0x7FC7)
  • R2014b -> 28672 (0x7000)
  • R2015aSP1 -> 32640 (0x7F80)
  • R2015b -> errors already even though release notes for R2016a say it should still run
  • R2016a -> errors already even though release notes for R2016a say it should still run

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by