- If your code is a script, you can remove varargin{:} and just use parse(p,DEM);
- If your code is a function and you need to use varargin to handle input arguments that may or may not be passed in, then you should define varargin in the function definition, as shown below, for example.
varargin error for optional inputs
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
Voss
le 27 Juin 2024
The problem is that varargin is not defined. Typically varargin refers to input arguments passed to a function. The code you are running appears to be a script, so varargin is not needed, since scripts take no input arguments.
function DEM = test_DEM(DEM,varargin)
%%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;
end
References:
2 commentaires
Voss
le 28 Juin 2024
You're welcome! Any questions, please let me know. Otherwise, please Accept this answer. Thanks!
Plus de réponses (1)
Ashutosh Thakur
le 27 Juin 2024
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 commentaire
Stephen23
le 27 Juin 2024
abc(1,2)
function abc(varargin)
disp(varargin{1})
disp(varargin{2})
end
Voir également
Catégories
En savoir plus sur Argument Definitions dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!