Question

I was just writing a Matlab function and wanted to copy and paste what I had to an interactive session. Some of it was nargin statements (e.g. if nargin < 1; a = 0; end;), and it turns out that nargin had the value 11005 in my workspace, without my having assigned it. Does anyone know what this is, whether it is used for anything (outside of functions) and if there is any problem with setting it to zero?

Was it helpful?

Solution

When used within a function, nargin gives the number of parameters passed to that function. Used with a string argument fn it is an in-built function, which returns the number of parameters taken by function fn. You should not call it without a parameter from the workspace:

nargin returns the number of input arguments passed in the call to the currently executing function. Use this nargin syntax only in the body of a function.

You can, but you should avoid assigning a value to nargin, since it will then loose the second semantics:

nargin('sparse')

ans =

 6

nargin = 0;
nargin('sparse')
Index exceeds matrix dimensions.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top