Is it possible to have function handles with optional arguments in Matlab
81 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Muhammad Imron
le 12 Août 2021
Commenté : Walter Roberson
le 12 Août 2021
I have about 25-50 Matlab function handles saved in a cell array so I can evaluate each of them using a for loop. Those function handles were converted from symbolic expression.
However, some of the function handles need 4 arguments, other need 2 arguments and few do not need any argument (they are constants). For example, some them need (x1,x2,y1,y2) as the arguments, some need(x1,x2), some need (y1,y2) and few need nothing.
My question: is it possible to have function handles with optional arguments in Matlab? which means I can give 4 arguments to the all function handles and let them decide if they need all or not. If they don't need some of them, they can just simply ignore them.
Thank you.
2 commentaires
Walter Roberson
le 12 Août 2021
No that does not solve the problem. The = form converts to name/value option pairs.
Réponse acceptée
Walter Roberson
le 12 Août 2021
Yes, anonymous functions can use varargin.
Note that matlabFunction will not generate varargin. It does, however, support specifying variable names in 'vars' that do not occur in the expression. It is absolutely fine to say
matlabFunction(y^2, 'vars', [x, y]) in which case the first input will be ignored
2 commentaires
Walter Roberson
le 12 Août 2021
syms x1 x2 y1 y2
matlabFunction(y1^2 - x2^2, 'vars', [x1, x2, y1, y2])
You still need to pass something in those positions, but whatever you pass will be ignored.
Plus de réponses (2)
Image Analyst
le 12 Août 2021
Modifié(e) : Image Analyst
le 12 Août 2021
It's certainly possible with a regular function. See varargin and nargin in the help. Can you use a regular function? You can still get a handle to a regular function just like you can with an anonymous function.
For an anonymous function, I don't know. I don't think it's possible. If you want to do it for a regular, normal function, let me know.
Chunru
le 12 Août 2021
With anonymous function, you can take in the argument in workspace instead of function arguments.
a = 1; b = 2; c = 1;
f = @(x) a*x.^2 + b*x + c;
y = f(2) % one function arguments; a b c are in workspace
1 commentaire
Image Analyst
le 12 Août 2021
I think what he was thinking regarding optional arguments is for you to not pass them in or specify them, and for those variables to take on default values that the function author decided upon in advance.
a = 1; b = 2;
f = @(x, c) a*x.^2 + b*x + c;
% Call without specifying c and having it use some default value for c.
% Case 1: arguments a & b are in workspace. Use (for example) 9 for c instead of 1.
y = f(2) % Won't work - c not specified
But like you pointed out, the way to do that is to just specify the value you want for c in advance:
% Case 2: arguments a, b, & c are in workspace. Use 1 for c since that is what we specified.
c = 1;
y = f(2, c) % Works since the "default" arguments a, b, and c are in the workspace.
% or even declare f() with no c at all and don't pass in anything.
f = @(x) a*x.^2 + b*x + c; % Use c value from workspace.
y = f(2)
Or of course you could just use a normal function instead of an anonymous one (which is what I'd do).
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!