error associated with Nargin

조회 수: 6 (최근 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 CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by