How to combine argument validation with varargin
86 次查看(过去 30 天)
显示 更早的评论
How can I make varargin work in conjunction with an arguments block? In the function below, the first two inputs W and p have very specific validation criteria. However, I have additional arguments, which I am trying to capture with varargin, with no validation criteria that I would like to enforce at this point.
someFunc(5,1,'dog',{2},6)
function someFunc(W,p, varargin)
arguments
W {mustBeNumeric}
p int8 {mustBeScalarOrEmpty,mustbeInteger}=2
varargin
end
W,p, varargin
end
As shown, the call throws an error saying that varargin can only be used inside Repeating arguments blocks, but I can find no relevant documentation or examples on that. Removing varargin from the arguments block altogether also generates an error:
File: test.m Line: 5 Column: 24
Function argument definition error in someFunc. Arguments block and function line must contain the same arguments in the
same order, including ignored arguments.
Is there simply no way to use varargin and arguments in conjunction?
0 个评论
采纳的回答
Paul
2023-12-22
My understanding from reading Avoid Using varargin for Repeating Arguments is that putting varargin into a Repeating arguments block without any restrictions would allow the code to work.
someFunc(5,1,'dog',{2},6)
function someFunc(W,p, varargin)
arguments
W {mustBeNumeric}
p int8 {mustBeScalarOrEmpty,mustBeInteger}=2
end
arguments (Repeating)
varargin
end
W,p, varargin
disp('vargin elements are:')
for ii = 1:numel(varargin)
varargin{ii}
end
end
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Argument Definitions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!