Input argument error length of variables
Show older comments
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 Comments
Massimo Zanetti
on 4 Oct 2016
You should upload your code otherwise how can we solve your problem? We cannot guess.......
Geoff Hayes
on 4 Oct 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.
Answers (1)
Marc Jakobi
on 7 Oct 2016
Edited: Marc Jakobi
on 7 Oct 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 Comments
Elizabeth Olson
on 7 Oct 2016
Elizabeth Olson
on 7 Oct 2016
Edited: Walter Roberson
on 9 Oct 2016
Marc Jakobi
on 7 Oct 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
on 9 Oct 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
on 9 Oct 2016
Nice, I was not aware of that new feature.
Categories
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!