Attempt to execute SCRIPT varargin as a function

48 views (last 30 days)
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.

Answers (3)

Wayne King
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.
  2 Comments
Elizabeth
Elizabeth on 8 May 2012
okay, but how do I do this?
In the Matlab help menu I found the following:
"Define a function in a file named varlist2.m that expects inputs X and Y, and accepts a variable number of additional inputs.
function varlist2(X,Y,varargin)"
which to me suggests that the syntax that I have [someClass(filename, pathname, varargin)] is the same as above.
Elizabeth
Elizabeth on 8 May 2012
The help menu defines varargin as:
"varargin is an input variable in a function definition statement that allows the function to accept any number of input arguments. Specify varargin using lowercase characters, and include it as the last input argument after any explicitly declared inputs"
this is exactly what I have done?

Sign in to comment.


Wayne King
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.
  1 Comment
Elizabeth
Elizabeth on 8 May 2012
Thanks for your help.
The following is writen in the code to define the varargin:
if ~isempty(varargin)
obj.a=varargin{1};
obj.b=varargin{2};
obj.c=varargin{3};
obj.d=varargin{4};
end
but maybe I am running the code out of sequence and that is why it is coming up as undefined. I am new to object oriented programing (which is how this particluar code is written).

Sign in to comment.


Wayne King
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

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!