How to use the "parse" function to create optional function inputs
Afficher commentaires plus anciens
Hello,
I am trying to use "parse" to create optional function inputs. Tried to replicate examples in the documentation (see below and attached) but I get the "too many arguments" error. Any idea? Thanks
my code below:
----------------
function []=testFun(A,varargList)
p = inputParser;
defaultNorm = 'L2';
validNorms = {'L1','L2'};
checkNorm = @(x) any(validatestring(x,validNorms));
addOptional(p,'lambda',0,@isnumeric)
addOptional(p,'norm',defaultNorm,checkNorm)
parse(A,varargList{:})
lambda = p.Results.lambda;
norm = p.Results.norm;
disp('input1 = ')
disp(A)
disp('lambda = ')
disp(lambda)
disp('norm = ')
disp (norm)
end
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 5 Fév 2017
1 vote
The variable name has to be varargin to be recognized as special, not varargList
1 commentaire
Francois Steiner
le 6 Fév 2017
Modifié(e) : Francois Steiner
le 6 Fév 2017
Catégories
En savoir plus sur Argument Definitions dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!