File Exchange

image thumbnail

Direct Indexing of Function Calls (OOP Exercise)

version (13 KB) by Matt J
Pseudo- function handle which can both call a function and post-index the output in 1 expression.

1 Download

Updated 13 Mar 2010

View License

IndexableFunction is a class of function-handle-like objects that allow a function to be called and post-indexed in a single expression.

Often, in the MATLAB Central NG, I've seen people ask whether it is possible to call functions with a syntax like y=func(arg)(i) as an alternative to doing,


Essentially, the answer is no. Standard MATLAB syntax does not allow this. However, the IndexableFunction class enables functions to be called with the very similar syntax y=func{arg}(i).

If you have to call and post-index a function multiple times, this might save you some keystrokes. However, the main benefit of this submission is probably as an illustration/exercise in MATLAB OOP. Also, it will give me a link to refer to whenever the discussion point pops up again in the NG (as it persistently does).

In general, there is no computational superiority that this syntax does or ever could bestow. Internally, the complete vector-valued output of the function is generated and then post-indexed, which is the only generic way of enabling this syntax for an arbitrary set of functions. This is because many MATLAB functions use algorithms that inherently must return a complete vector-valued output (e.g. fft(x)).




h: a function handle


f: an IndexableFunction object


>>hsin=@sin; %Handle to sine function

>>fsin=IndexableFunction(@sin); %Create object

>>hsin((0:.25:1)*pi) %An ordinary kind of function call

ans =

0 0.7071 1.0000 0.7071 0.0000

%Equivalent function call using the object (note the braces)

ans =

0 0.7071 1.0000 0.7071 0.0000

%The same function call, but returning the 3rd and 4th component only.

ans =

1.0000 0.7071

The submission also contains some additional tools meant to allow a library of IndexableFunctions to be made globally available to all MATLAB workspaces.

There tool initlib.m creates a library data file LibraryStruct.mat. The file contains a structure S whose fields are IndexableFunction handles to all the methods of class double. You can also add/remove your own functions to/from the library using add2lib.m and rmlib.m (see the help doc for these functions).

There is also an additional class 'flib'. MATLAB automatically loads S from LibraryStruct.mat into a globally available Constant property of flib when the class is first accessed. This allows you to do things like the following 1-line operations in any MATLAB workspace,


x =

-0.3424 0.4706 0.4572 -0.0146


ans =



ans =


In cases where it is worthwhile, you can of course unpack IndexableFunctions that you are going to use repeatedly, e.g.


ans =


Cite As

Matt J (2020). Direct Indexing of Function Calls (OOP Exercise) (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (0)


Added flib.m, add2lib.m, rmlib.m, initlib.m all of which are explained in the revised Description section.

Whoops. Forgot to actually upload the update .zip

Added LibraryStruct (see revised Description section for details)

MATLAB Release Compatibility
Created with R2009b
Compatible with any release
Platform Compatibility
Windows macOS Linux