Previous: , Up: Calling Functions   [Contents][Index]


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.