Questions tagged [function-handle]

In Matlab, a function handle is a mechanism provided to allow for indirect calling of a function. Serves like a "pointer to function"

A function handle is a value that provides a means of calling a function indirectly.

159 questions
38
votes
3 answers

What is a function handle and how is it useful?

Can somebody explain to me the meaning of the @ (function handle) operator and why to use it?
Dikla
  • 3,461
  • 5
  • 30
  • 43
13
votes
3 answers

What is the @ operator (at sign) in MATLAB?

I have some MATLAB programs that use the @ (at sign) as an operator. What does it mean? Does MATLAB 6.5 support this operator?
Yin Zhu
  • 16,980
  • 13
  • 75
  • 117
11
votes
2 answers

Matlab: defining a function handle catching second returned value of a function

Say that I have a function foo defined as [a b] = foo(c ). If I consider a function handle f = @(c)foo(c) to be used e.g. in a cellfun call, what I get is an f behaving equivalently to a foo defined like a = foo(c) i.e., the returned…
Acorbe
  • 8,367
  • 5
  • 37
  • 66
10
votes
1 answer

Creating a function handle to an overloaded `end` function

MATLAB allows overloading various operators for custom classes. One of the unlisted overloadable operators is end, as can be learned from \matlab\lang\end.m: % END(A,K,N) is called for indexing expressions involving the object A % when END is…
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
10
votes
1 answer

MATLAB- passing a function handle parameter into another function as a handle

Working on an assignment involving Genetic Algorithms (loads of headaches, loads of fun). I need to be able to test differing crossover methods and differing mutation methods, to compare their results (part of the paper I have to write for the…
user194638
9
votes
3 answers

Matlab mex-file with mexCallMATLAB is almost 300 times slower than the corresponding m-file

I started implementing a few m-files in C++ in order to reduce run times. The m-files produce n-dimensional points and evaluate function values at these points. The functions are user-defined and they are passed to m-files and mex-files as function…
Meteor
  • 93
  • 1
  • 5
9
votes
1 answer

How can I create function pointers from a string input in MATLAB?

If I use the inline function in MATLAB I can create a single function name that could respond differently depending on previous choices: if (someCondition) p = inline('a - b','a','b'); else p = inline('a + b','a','b'); end c = p(1,2); d =…
JP.
  • 5,507
  • 15
  • 59
  • 100
8
votes
1 answer

Matlab - Check if function handle is a particular function or function type

Question: In Matlab, how can I check if a function handle is a particular function or function type? Example: Let f1 be a function handle. How do I check if f1 is the in-built Matlab function mean? How do I check if f1 is an anonymous function? My…
Colin T Bowers
  • 18,106
  • 8
  • 61
  • 89
7
votes
3 answers

When can I pass a function handle?

I have a function for cached evaluation. As one of the arguments, it takes a function handle. Under some circumstances, the function handle is unaccessible, and I don't quite understand why. The example below shows what got me stumped: >> A.a =…
gerrit
  • 24,025
  • 17
  • 97
  • 170
6
votes
4 answers

Get the derivative of a function_handle in MATLAB

Is it possible to get the derivative of a function_handle as a other function_handle? Like: fun1 = @(x) x^2; % do that ... disp(fun2); @(x) x*2 I know how to find the derivative of a symbolic function but I can't convert a function_handle…
user839791
  • 671
  • 1
  • 6
  • 5
6
votes
2 answers

Conditional element replacement using cellfun

vec = randi(10,10,1) vec(vec < 5) = 0 func = @(x) x(x < 5) = 0 % This isn't valid How am I supposed to translate the second line of code into a function handle that I can use in conjunction with cellfun?
6
votes
2 answers

How to write an anonymous function with a variable number of output arguments?

Using deal we can write anonymous functions that have multiple output arguments, like for example minmax = @(x)deal(min(x),max(x)); [u,v] = minmax([1,2,3,4]); % outputs u = 1, v = 4 But if you want to provide a function with its gradient to the…
flawr
  • 10,814
  • 3
  • 41
  • 71
6
votes
5 answers

Calling matlab callback/function handle from Java

How do I pass a matlab function handle to a Java object and invoke it from within Java (that is, I want Java to tell matlab when it is ready with a calculation). I am trying to use the com.mathworks.jmi.Matlab class for evaluating Matlab expressions…
jakob
5
votes
1 answer

Why are predefined variables not shown with their value in function handles?

In MATLAB R2020b I have the following code: f=@(x) x.^2; y=2; g=@(x) f(x).*y and the output is g = function_handle with value: @(x)f(x).*y But y = 2, so I would expect the output to be @(x)f.*2. Is this how its supposed to work or a bug? And can I…
Matt Majic
  • 381
  • 9
  • 18
5
votes
1 answer

Is it possible to test a function handle without try block?

Is is possible to replace the following code with something which does not use exceptions? The handle x is a provided handle. I want to test it for validity (having actual code to back the handle) before use. x = @notreallyafunction; try …
Andrej Mikulik
  • 569
  • 5
  • 14
1
2 3
10 11