Attempt to execute SCRIPT varargin as a function
조회 수: 4 (최근 30일)
이전 댓글 표시
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
답변 (3개)
Wayne King
2012년 5월 8일
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
2012년 5월 8일
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
2012년 5월 8일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!