Create an account Log In

# Direct Indexing of Function Calls (OOP Exercise) (Scripts) 1.0

Average Rating
User Rating:
Visitors Rating:
My rating:

• Last update: 5 years ago
• Total downloads: 37
• Operating system: Linux, Mac OS X, Win All, BSD, Solaris
• Publisher: Matt J (15 other programs)
See full specifications

## Direct Indexing of Function Calls (OOP Exercise) (Scripts) Publisher's description

### IndexableFunction is a class of function-handle-like objects

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

#### System Requirements:

MATLAB 7.9 (2009b)
Program Release Status: New Release
Program Install Support: Install and Uninstall

#### Direct Indexing of Function Calls (OOP Exercise) (Scripts) Tags:

Click on a tag to find related softwares

### Is Direct Indexing of Function Calls (OOP Exercise) (Scripts) your software?

Manage your software

## Most Popular

ASK, OOK, FSK, BPSK, QPSK, 8PSK modulation 1.1
ASK, OOK, FSK, BPSK, QPSK, 8PSK modulation contain several functions for digital modulation simulation
Simulink Communication Labs 1.1
Simulink Communication Labs allows you to learn communication systems in greater depth.
M-QAM modulation and demodulation 1.1
M-QAM modulation and demodulation is the QAM modulation and demodulation tech.
LZW Compression/Decompression 1.1
LZW Compression/Decompression - Updated LZW compressor and decompressor with reasonable performance
InSPIRE utility to plot a 2D displacement field (Scripts) 1.0
This program plots the deformation field (displace vectors) contained in vector.txt.