Home > atmlab > handy > par.m

par

PURPOSE ^

PAR function equivalent of parenthesis

SYNOPSIS ^

function y = par(x, varargin)

DESCRIPTION ^

 PAR function equivalent of parenthesis

 Circumvent Matlabs syntactic limitation on indexing directly on the
 result of an expression.  If an expression ends in round or curly braces,
 such as after a function call, Matlab forces you to store the result
 in a variable (or use `subsref` ugliness) to access it.  This may be
 painful.  So where you would want to write:

   i = magic(3)(1, 2);

 and you would previously write:

   M = magic(3);
   i = M(1, 2);

 this function allows you to write instead:

   i = par(magic(3), 1, 2);

 It works for either arrays or function calls, so it allows for
 arbitrarily deep function chaining if you call p recursively.
 Ranges work, e.g. p(magic(3), 1:2, 2).  Full ranges appear (a lone ':')
 appear to work under particular circumstances, but always work when you
 pass in ':' as a string.  As far as I know, there is no equivalent of
 'end'.

 FORMAT

   y = par(x, a, b, ...)

 IN

   x           matrix to be indexed
   a, b, ...   index per dimension

 OUT

   y           result of indexing operation x(a, b, ...)

 By Gerrit Holl, inspired by http://is.gd/b3MkRf

 See also: cur, getfield

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

DOWNLOAD ^

par.m

SOURCE CODE ^

0001 function y = par(x, varargin)
0002 % PAR function equivalent of parenthesis
0003 %
0004 % Circumvent Matlabs syntactic limitation on indexing directly on the
0005 % result of an expression.  If an expression ends in round or curly braces,
0006 % such as after a function call, Matlab forces you to store the result
0007 % in a variable (or use `subsref` ugliness) to access it.  This may be
0008 % painful.  So where you would want to write:
0009 %
0010 %   i = magic(3)(1, 2);
0011 %
0012 % and you would previously write:
0013 %
0014 %   M = magic(3);
0015 %   i = M(1, 2);
0016 %
0017 % this function allows you to write instead:
0018 %
0019 %   i = par(magic(3), 1, 2);
0020 %
0021 % It works for either arrays or function calls, so it allows for
0022 % arbitrarily deep function chaining if you call p recursively.
0023 % Ranges work, e.g. p(magic(3), 1:2, 2).  Full ranges appear (a lone ':')
0024 % appear to work under particular circumstances, but always work when you
0025 % pass in ':' as a string.  As far as I know, there is no equivalent of
0026 % 'end'.
0027 %
0028 % FORMAT
0029 %
0030 %   y = par(x, a, b, ...)
0031 %
0032 % IN
0033 %
0034 %   x           matrix to be indexed
0035 %   a, b, ...   index per dimension
0036 %
0037 % OUT
0038 %
0039 %   y           result of indexing operation x(a, b, ...)
0040 %
0041 % By Gerrit Holl, inspired by http://is.gd/b3MkRf
0042 %
0043 % See also: cur, getfield
0044 
0045 % $Id: par.m 8777 2014-02-12 12:45:13Z gerrit $
0046 y = x(varargin{:});
0047 end

Generated on Mon 15-Sep-2014 13:31:28 by m2html © 2005