Passing arguments through a function

63 Ansichten (letzte 30 Tage)
Florian Rössing
Florian Rössing am 7 Dez. 2022
Beantwortet: Steven Lord am 9 Dez. 2022
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!

Antworten (3)

Steven Lord
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 = 1×10
1 4 9 16 25 36 49 64 81 100
y = fun1873132(@(x, a, b, c) x.*a.^2 + x.*b + c, 3, 2, 1)
y = 1×10
12 23 34 45 56 67 78 89 100 111
check = (1:10).*3^2 + 2*(1:10) + 1
check = 1×10
12 23 34 45 56 67 78 89 100 111
function y = fun1873132(fh, varargin)
arguments
fh (1, 1) function_handle
end
arguments(Repeating)
varargin
end
y = fh(1:10, varargin{:});
end

Torsten
Torsten am 7 Dez. 2022
Bearbeitet: Torsten am 7 Dez. 2022
x = -5:1:5
x = 1×11
-5 -4 -3 -2 -1 0 1 2 3 4 5
disp(TopFunction(@(x)lin(x,1,2),x))
-3 -2 -1 0 1 2 3 4 5 6 7
disp(TopFunction(@(x)square(x,1),x))
25 16 9 4 1 0 1 4 9 16 25
function y = TopFunction(func,x)
y = func(x);
end
function y =lin(x,a,b)
y = a*x+b;
end
function y =square(x,a)
y = a*x.^2;
end
  2 Kommentare
Florian Rössing
Florian Rössing am 9 Dez. 2022
Thank you, this works in this case. But is there no more general way? When I have more complex nested functions?
Torsten
Torsten am 9 Dez. 2022
Bearbeitet: Torsten am 9 Dez. 2022
MATLAB is not C++.
If you have an application that needs structured programming, you should use a programming environment that better fits this purpose than MATLAB does.

Melden Sie sich an, um zu kommentieren.


Stephen23
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)))
-3 -2 -1 0 1 2 3 4 5 6 7
disp(TopFun(@square,x,struct(a=1)))
25 16 9 4 1 0 1 4 9 16 25
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

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by