Effacer les filtres
Effacer les filtres

varargin error for optional inputs

49 vues (au cours des 30 derniers jours)
Uma Narayan
Uma Narayan le 27 Juin 2024 à 6:37
Commenté : Voss le 28 Juin 2024 à 6:14
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

Réponse acceptée

Voss
Voss le 27 Juin 2024 à 14:25
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.
  • 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.
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
Uma Narayan
Uma Narayan le 28 Juin 2024 à 5:21
Thanks.This works for me.
Voss
Voss le 28 Juin 2024 à 6:14
You're welcome! Any questions, please let me know. Otherwise, please Accept this answer. Thanks!

Connectez-vous pour commenter.

Plus de réponses (1)

Ashutosh Thakur
Ashutosh Thakur le 27 Juin 2024 à 8:27
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)
1 2
I hope that this helps you!
  1 commentaire
Stephen23
Stephen23 le 27 Juin 2024 à 8:51
abc(1,2)
1 2
function abc(varargin)
disp(varargin{1})
disp(varargin{2})
end

Connectez-vous pour commenter.

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!

Translated by