Passing in a function as an argument

458 visualizaciones (últimos 30 días)
Kaloyan Marinov
Kaloyan Marinov el 18 de Abr. de 2014
Comentada: Steven Lord el 24 de Jun. de 2022
Dear all,
I would like to write a Function M-file f.m, which is to contain a primary function called f as well as two subfunctions called subFunc1 and subFunc2, respectively.
Within the primary function f, I need to call subFunc1; what is the proper syntax for passing subFunc2 as an input parameter to such a call of subFunc1?
Any feedback would be very much appreciated.
Regards, Kaloyan

Respuestas (4)

Ted Shultz
Ted Shultz el 20 de Ag. de 2019
you use the "@" character to pass a function, not the output of a funcitn. This looks like this:
function main
clc
testCode(@sum, [1 2 3]) % calls "testCode" with builtin matlab function "sum"
testCode(@doubleSum, [1 2 3]) % calls "testCode" with custom function defined below
end
function valOut = testCode(funcIn, numIn)
% funcIn is a function passed to this function! you call as you would the passed function:
valOut = funcIn(numIn);
end
function ds = doubleSum(numIn)
ds = 2*sum(numIn);
end
  2 comentarios
Abdul Hanan Wali
Abdul Hanan Wali el 24 de Jun. de 2022
and what if we have to compare them. like if ds == valOut it should return true
Steven Lord
Steven Lord el 24 de Jun. de 2022
Comparing functions or function handles is unlikely to be what you want.
Now comparing the results of evaluating a function or function handle, on the other hand, is often useful.
f1 = @sin;
f2 = @(x) cos(pi/2-x);
x = [0 pi 2*pi];
norm(f1(x)-f2(x)) < 1e-6 % Don't use == for floating point comparison
ans = logical
1
Note that directly trying to compare f1 and f2 won't work.
f1 == f2 % error
Operator '==' is not supported for operands of type 'function_handle'.

Iniciar sesión para comentar.


Stuart Kozola
Stuart Kozola el 18 de Abr. de 2014
To pass in a function to another function, use function handles (@).
sf2 = @(inputs) subFcn2(inputs)
then call it in subFcn1 as
out1 = subFcn1(sf2)
or alternatively
out2 = subFcn1(@subFcn)
Note that you add @ to the function name to declare it as a function (vs. a variable) to be passed into the function.
Now if you have constant parameters that need to be passed into subFcn1, you can define the sf2 as
sf2 = @(input) subFcn(input,param1,param2)
and to call sf2 it only takes in input
out2 = sf2(input)
Note that the constant parameters are already included in the definition of sf2 which only takes in one input.
If you need more explaination or examples, look at the doc:

Azzi Abdelmalek
Azzi Abdelmalek el 18 de Abr. de 2014
a=@subFunc2
Then call your function with a argument
  1 comentario
Kaloyan Marinov
Kaloyan Marinov el 18 de Abr. de 2014
Editada: Kaloyan Marinov el 18 de Abr. de 2014
Do you mean I should pass subFunc2 as an input parameter to a call of subFunc1 it as follows:
a = @subFunc2;
subFunc1(a);
?

Iniciar sesión para comentar.


Omkar Dumne
Omkar Dumne el 9 de Abr. de 2021
How does matlab help help in passing function arguments

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by