Be the first to rate this file! 20 downloads (last 30 days) File Size: 10.93 KB File ID: #26570

Direct Indexing of Function Calls (OOP Exercise)

by Matt J

 

02 Feb 2010 (Updated 08 Feb 2010)

Code covered by the BSD License  

Pseudo- function handle which can both call a function and post-index the output in 1 expression.

Download Now | Watch this File

File Information
Description

 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:
 
          >>h=@sin; %Handle to sine function
 
          >>f=IndexableFunction(h); %Create object
 
          >>h((0:.25:1)*pi) %An ordinary kind of function call
  
          ans =
  
                   0 0.7071 1.0000 0.7071 0.0000
  
          >>f{(0:.25:1)*pi}
               %Equivalent function call using the object (note the braces)
  
          ans =
  
                   0 0.7071 1.0000 0.7071 0.0000
  
  
          >>f{(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 a file LibraryStruct.mat (and the file LibraryStruct.m that created it) with a potentially useful construct. The file contains a structure S whose field names are all the methods of class @double. Each field is an IndexableFunction handle to the corresponding method. This allows you to do things like this:
 
      >> load LibraryStruct
      >>x=rand(1,4)-.5 %some data
  
      x =
  
         -0.3424 0.4706 0.4572 -0.0146
  
      >>S.abs{x}(1)
  
      ans =
  
          0.3424
  
      >>S.sort{x}(2)
  
      ans =
  
         -0.0146

MATLAB release MATLAB 7.9 (2009b)
Zip File Content  
Other Files IndexableFunction.m,
LibraryStruct.m,
LibraryStruct.mat,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Updates
06 Feb 2010

Added LibraryStruct (see revised Description section for details)

08 Feb 2010

Whoops. Forgot to actually upload the update .zip

Tag Activity for this File
Tag Applied By Date/Time
fancy function Matt J 03 Feb 2010 09:24:45
indexing Matt J 08 Feb 2010 09:31:11
function Matt J 08 Feb 2010 09:31:11
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com