Input argument error length of variables
Mostrar comentarios más antiguos
I am trying to use the following script to process some of my data. I keep getting this error:
Error using nargin
You can only call nargin/nargout from within a MATLAB function.
function [corrected, details] = pin(year,d13c,alpha,varargin)
↑
Error: Function definitions are not permitted in this context.
] This code was written in 2009 so I don't know if it is outdated or what is going on. I need this to work so any help would be greatly appriciated!
2 comentarios
Massimo Zanetti
el 4 de Oct. de 2016
You should upload your code otherwise how can we solve your problem? We cannot guess.......
Geoff Hayes
el 4 de Oct. de 2016
Elizabeth - the second error message suggests that you have some code that precedes the function definition/signature. If you are defining a function, then it must be the first line in your file.
Respuestas (1)
Marc Jakobi
el 7 de Oct. de 2016
Editada: Marc Jakobi
el 7 de Oct. de 2016
The problem is exactly what the error message says:
Somewhere in your script is the keyword "nargin". nargin is used to check the amount of inputs of a function and because of that it can only be used within a function, not within a script.
Example:
function numinputs = checkinputs(a, b, c)
if nargin == 1
numinputs = 1; %a
elseif nargin == 2
numinputs = 2; %a and b
else
numinputs = 3; %a, b and c
end
end
5 comentarios
Elizabeth Olson
el 7 de Oct. de 2016
Elizabeth Olson
el 7 de Oct. de 2016
Editada: Walter Roberson
el 9 de Oct. de 2016
Marc Jakobi
el 7 de Oct. de 2016
Please post Matlab code formatted as code. It is currently not readable.
The problem is that it is a function and not a script. You cannot run a function like you would run a script by pressing the run button. You should call the function from within a script or from the command window as:
[corrected, details] = pin(year,d13c,alpha,varargin)
where year, d13c and alpha are your data.
Type
doc pin
in the command window to find out how to use it.
Walter Roberson
el 9 de Oct. de 2016
All of the code starting from 'function' should be stored in pin.m . You can then call upon pin from the command line or from a script. You cannot paste the code in at the command prompt.
If you are using any version before R2016b then it is not permitted to store a function after a script in the same file.
If you are using the new R2016b then it is permitted to store a function after a script in the same file. In such a case the .m file name must not be the name of any function that is stored in the .m file.
Marc Jakobi
el 9 de Oct. de 2016
Nice, I was not aware of that new feature.
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!