Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Do stuff without colons
Date: Tue, 9 Dec 2008 21:50:24 +0000 (UTC)
Organization: Xoran Technologies
Lines: 52
Message-ID: <ghmp70$c4k$1@fred.mathworks.com>
References: <ghmkpn$9ml$1@fred.mathworks.com> <ghmlta$s44$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1228859424 12436 172.30.248.37 (9 Dec 2008 21:50:24 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 9 Dec 2008 21:50:24 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1440443
Xref: news.mathworks.com comp.soft-sys.matlab:505972


"Matt Fig" <spamanon@yahoo.com> wrote in message <ghmlta$s44$1@fred.mathworks.com>...
> I have wanted a built-in operator that does this kind of thing for a while.  Something like ->
> 
> b = randperm(100) ->(1:10);
> 
> This comes up when creating a cell array for no purpose except to use {:} too.  But we don't have one.

You can pretty much get this effect by wrapping any function handle in an object with an overloaded subsref method. Something like below (note: I'm still using old-style class definitions...)

%%%The functions below would go in an a class directory called @IndexableFunction

function f=IndexableFunction(h)
%Class constructor
%
%h is a handle to a function which object f will represent

...
f.h=h;
f=class(f,'IndexableFunction');
...

%%%%
function out=subref(f,S)
%SUBSREF method for @IndexableFunction class
%
%The function is called by passing arguments in braces
%
%    A=f{arg1,arg2,arg3,...}
%
% is equivalent to f.h(arg1,arg2,arg3,...)
%
%Subsequent indexing of the output will also be be possible, e.g.
%
%  f{arg1,arg2,...}(1:10)
%
%will be equivalent to A(1:10) 

switch S(1).type

  case '{}'
    out=f.h(S(1).subs{:});

  otherwise
   error 'Bad syntax'

end

 out=subsref(out,S(2:end));