varargin error for optional inputs
显示 更早的评论
I was running this code
%%Parse Inputs
p = inputParser;
p.FunctionName = 'linear_inversion_ksn';
% required inputs
addRequired(p,'DEM', @(x) isa(x,'GRIDobj'));
% optional inputs
addOptional(p,'crita', 1e6, @(x) isscalar(x));
addOptional(p,'mn', 0.5, @(x) isscalar(x));
addOptional(p,'chi_inc', 1, @(x) isscalar(x));
addOptional(p,'gam', 10, @(x) isscalar(x));
addOptional(p,'flowOption', []);
parse(p,DEM, varargin{:});
DEM = p.Results.DEM;
And got an error like this
>> parse(p,DEM, varargin{:});
Brace indexing into the result of a function call is not supported. Assign the result
of 'varargin' to a variable first, then brace index into it.
Kindly help me in fixing it
采纳的回答
更多回答(1 个)
Hello Uma,
The error you are facing is due to the way in which MATLAB handles the varargin input. I can see that you are trying to pass varargin with brace indexing to the parse function. This behavior is not supported which is causing the issue. Instead, I recommend assigning the varargin to the different variable and pass that variable with the brace indexing to the parse function. https://www.mathworks.com/help/matlab/ref/varargin.html.
Following sample code can be referred for the above-mentioned approach:
function abc(varargin)
% Assign varargin to a variable first
vararginCell = varargin;
disp(vararginCell{1})
disp(vararginCell{2})
end
abc(1,2)
I hope that this helps you!
1 个评论
abc(1,2)
function abc(varargin)
disp(varargin{1})
disp(varargin{2})
end
类别
在 帮助中心 和 File Exchange 中查找有关 Argument Definitions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!