在以下函数中,我尝试将Pt设置为可选输入参数。如果未指定Pt,则需要计算其他可选参数(该部分有效)。但是当我指定它时:
Alg(b,'circle','Pt',ones(150,1))
我收到以下错误:
'Pt'不是公认的参数。有关有效的名称-值对参数的列表,请参见此函数的文档。
功能代码:
function [ v ] = Alg( b,shape,varargin )
%%Parse inputs
p = inputParser;
addRequired(p,'b',@isnumeric);
expectedShapes = {'square','circle'};
addRequired(p,'shape',@(x) any(validatestring(x,expectedShapes)));
defaultIt = 42;
addParameter(p,'It',defaultIter,@isnumeric);
addParameter(p,'t',@isnumeric);
addParameter(p,'v',@isnumeric);
parse(p,b,shape,varargin{:})
b = p.Results.b;
shape = p.Results.shape;
It = p.Results.It;
t = p.Results.t;
v = p.Results.v;
parse(p,b,shape,varargin{:})
defaultPoint = Alg_sq(b,Pt,It);
defaultPoint = Sub_Alg(defaultPoint,shape,t,v);
addParameter(p,'Pt',defaultPoint,@isnumeric);
Pt = p.Results.Pt;
%%Shape
switch shape
case 'circle'
v = Alg_crcl( b,Pt,It );
case 'square'
v = Alg_sq( b,Pt,It );
end
end
非常感谢你的帮助!
发生错误是因为Pt
最初分析参数时未将其指定为有效的参数名称。您需要稍微重新组织代码,这是我的方法:
function v = Alg(b, shape, varargin)
% define arguments
p = inputParser;
addRequired(p, 'b', @isnumeric);
addRequired(p, 'shape', @(x) any(validatestring(x,{'square','circle'})));
addParameter(p, 'It', 42, @isnumeric);
addParameter(p, 't', @isnumeric);
addParameter(p, 'v', @isnumeric);
addParameter(p, 'Pt', [], @isnumeric); % default for now is empty matrix
% parse arguments
parse(p, b, shape, varargin{:})
b = p.Results.b;
shape = p.Results.shape;
It = p.Results.It;
t = p.Results.t;
v = p.Results.v;
Pt = p.Results.Pt;
if isempty(Pt)
% insert your logic to compute actual default point
% you can use the other parsed parameters
Pt = computeDefaultValue();
end
% rest of the code ...
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句