Azzera filtri
Azzera filtri

Is it possible to have function handles with optional arguments in Matlab

81 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Walter Roberson
Walter Roberson il 12 Ago 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 Commenti
Walter Roberson
Walter Roberson il 12 Ago 2021
syms x1 x2 y1 y2
matlabFunction(y1^2 - x2^2, 'vars', [x1, x2, y1, y2])
ans = function_handle with value:
@(x1,x2,y1,y2)-x2.^2+y1.^2
You still need to pass something in those positions, but whatever you pass will be ignored.

Accedi per commentare.

Più risposte (2)

Image Analyst
Image Analyst il 12 Ago 2021
Modificato: Image Analyst il 12 Ago 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.
  1 Commento
Muhammad Imron
Muhammad Imron il 12 Ago 2021
Yes, I know to handle this with a regular function. I could do this with regular functions but that would be tedious since I have many functions at hand (can be more than 25 functions). Thank you anyway.

Accedi per commentare.


Chunru
Chunru il 12 Ago 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
y = 9
  1 Commento
Image Analyst
Image Analyst il 12 Ago 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).

Accedi per commentare.

Categorie

Scopri di più su Argument Definitions in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by