8.2.3 Access via Handle

A function may be abstracted and referenced via a function handle acquired using the special operator ‘@’. For example,

f = @plus;
f (2, 2)
⇒  4

is equivalent to calling plus (2, 2) directly. Beyond abstraction for general programming, function handles find use in callback methods for figures and graphics by adding listeners to properties or assigning pre-existing actions, such as in the following example:

function mydeletefcn (h, ~, msg)
  printf (msg);
endfunction
sombrero;
set (gcf, "deletefcn", {@mydeletefcn, "Bye!\n"});
close;

The above will print "Bye!" to the terminal upon the closing (deleting) of the figure. There are many graphics property actions for which a callback function may be assigned, including, buttondownfcn, windowscrollwheelfcn, createfcn, deletefcn, keypressfcn, etc.

Note that the ‘@’ character also plays a role in defining class functions, i.e., methods, but not as a syntactical element. Rather it begins a directory name containing methods for a class that shares the directory name sans the ‘@’ character. See Object Oriented Programming.

© 1996–2020 John W. Eaton
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://octave.org/doc/v6.3.0/Access-via-Handle.html