Passing arguments through a function
63 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi Everyone,
I was looking around for this but could not find an answer. I want to build a function that takes a function handle and a list of arguments as input:
function y = TopFunction(func,x,args)
arguments
func function_handle
x;
args;
end
y = func(x,args);
end
and be able to pass different functions into it, like:
function y =lin(x,a,b)
y = a*x+b;
end
function y =square(x,a)
y = a*x^2;
end
x = -5:1:5
disp(TopFunction(@lin,x,a=1,b=2))
disp(TopFunciton(@square,x,a=1))
Is there a way to do something like that?
The Name-Value argumetn syntax as stated here requires me to specify the name value arguments names that are possible, which is unpractical for a large number of different possible functions.
There is a validation based on class properties here. Is there maybe something similar for function arguments?
Thanks in Advance!
0 Kommentare
Antworten (3)
Steven Lord
am 9 Dez. 2022
You could do this using varargin, but if you do make sure you like the function's interface before you give it to anyone else. Because all the trailing input arguments are passed into the function handle you specify as the first input, adding any new input arguments could require users of your code to change how they call your function. The ODE solvers used to use this type of approach 20 years ago, but for this (and other) reason we no longer recommend or document that syntax, instead recommending that you encourage users of your function to parameterize their function handles.
y = fun1873132(@(x, a) x.^a, 2)
y = fun1873132(@(x, a, b, c) x.*a.^2 + x.*b + c, 3, 2, 1)
check = (1:10).*3^2 + 2*(1:10) + 1
function y = fun1873132(fh, varargin)
arguments
fh (1, 1) function_handle
end
arguments(Repeating)
varargin
end
y = fh(1:10, varargin{:});
end
0 Kommentare
Stephen23
am 9 Dez. 2022
Bearbeitet: Stephen23
am 9 Dez. 2022
As I understand it, your goal is to alllow for an arbitrary number of named input arguments, without needing these to be explicitly given in the ARGUMENTS block. As far as I can tell from the documentation and some experimentation, all named arguments need to be declared.
You might be able to use the class-properties-based approach that you linked to, possibly in conjunction with a "universal" class, similar to the ones explained here:
Another option would be to avoid the situation by using nested functions (requires everything to be in one file).
Another approach is to use a structure to hold the parameters. Then all you need to do is pass around that structure. See also the examples here:
x = -5:1:5;
disp(TopFun(@lin,x,struct(a=1,b=2)))
disp(TopFun(@square,x,struct(a=1)))
function y = lin(x,s)
y = s.a*x+s.b;
end
function y = square(x,s)
y = s.a*x.^2;
end
function y = TopFun(func,x,s)
arguments
func function_handle
x
s struct
end
y = func(x,s);
end
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!