How to combine argument validation with varargin
87 ビュー (過去 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
参考
カテゴリ
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!