Attempt to execute SCRIPT varargin as a function
48 views (last 30 days)
Show older comments
Hello I am trying to run the follow OOP script
someClass(filename,pathname,varargin)
where the filename and pathname are known and "someClass" is a "someClass.m" class definition script.
However, I get the following error message:
Attempt to execute SCRIPT varargin as a function:
C:\Program Files\MATLAB\R2012a\toolbox\matlab\lang\varargin.m
The program will run if I just use
someClass(filename, pathname)
but I think I will have problems down the line when my number of inputs changes. This is code written by someone else.
thanks.
0 Comments
Answers (3)
Wayne King
on 8 May 2012
You want to actually pass input arguments, not use varagin as an input argument. varargin.m
The person has written that function to collect all the optional inputs after the first two in varargin.
Wayne King
on 8 May 2012
Inside the program you have to define what is acceptable for the varargin, or you have to use with with other MATLAB functions that have optional inputs. You are correct in how you use varargin in the function defintion, but you still have to specify what the acceptable arguments are inside the function.
For example:
function varx = myvar(x,varargin)
varx = var(x,varargin{:});
end
x = randn(10,2);
varx = myvar(x,[0.1 0.8],2);
Here the cell array varargin{:} takes the weight vector and the dimension.
Or
function sumx = mysum(x,varargin)
N = nargin;
if (N>1)
weights = varargin{1};
sumx = sum(x.*weights);
else
sumx = sum(x);
end
Then from the command line, you call the function:
x = randn(10,1);
sumx = mysum(x);
% or
sumx = mysum(x,0.1*ones(10,1));
Notice I do not pass varargin to the function. You can obviously extend the size of the cell array.
Wayne King
on 8 May 2012
This code says if you specify additional inputs beside the mandatory two, then assign the first to the "a" field of the structure array obj, assign the second to the field "b" and so on up to 4 additional inputs (fields, "a","b","c", and "d)
if ~isempty(varargin)
obj.a=varargin{1};
obj.b=varargin{2};
obj.c=varargin{3};
obj.d=varargin{4};
end
0 Comments
See Also
Categories
Find more on Software Development Tools 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!