## Direct Indexing of Function Calls (OOP Exercise)

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

Updated 13 Mar 2010

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,

z=func(arg);
y=z(i);

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)).

USAGE:

f=IndexableFunction(h)

in:

h: a function handle

out:

f: an IndexableFunction object

EXAMPLE:

>>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

>>fsin{(0:.25:1)*pi}
%Equivalent function call using the object (note the braces)

ans =

0 0.7071 1.0000 0.7071 0.0000

>>fsin{(0:.25:1)*pi}(3:4)
%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=rand(1,4)-.5

x =

-0.3424 0.4706 0.4572 -0.0146

>>flib.S.abs{x}(1)

ans =

0.3424

>>flib.S.sort{x}(2)

ans =

-0.0146

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

>>sort=flib.S.sort;
>>sort{x}(2)

ans =

-0.0146

### Cite As

Matt J (2023). Direct Indexing of Function Calls (OOP Exercise) (https://www.mathworks.com/matlabcentral/fileexchange/26570-direct-indexing-of-function-calls-oop-exercise), MATLAB Central File Exchange. Retrieved .

##### MATLAB Release Compatibility
Created with R2009b
Compatible with any release
##### Platform Compatibility
Windows macOS Linux
##### Categories
Find more on Whos in Help Center and MATLAB Answers

### Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.3.0.0

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

1.2.0.0

Whoops. Forgot to actually upload the update .zip

1.1.0.0

Added LibraryStruct (see revised Description section for details)

1.0.0.0