Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of slinvoke
Home > sltoolbox > utils > slinvoke.m

slinvoke

PURPOSE ^

SLINVOKE Invokes a function

SYNOPSIS ^

function varargout = slinvoke(invoke_descr, varargin)

DESCRIPTION ^

SLINVOKE Invokes a function

 $ Syntax $
   - [y1, y2, ..., yn] = slinvoke(invoke_descr, x1, x2, ..., xm)

 $ Arguments $
   - invoke_descr:       the invoking descriptor
   - x1, x2, ..., xm:    the input arguments
   - y1, y2, ..., yn:    the output arguments

 $ Description $
   - [y1, y2, ..., yn] = slinvoke(invoke_descr, placepos, x1, x2, ..., xm) 
     invokes some function with a specified manner. The invoking descriptor
     can be in following form:
     1. a string representing the function name, a function handle or an 
        inline object. In this case, no fixed argument is binded. The x1, 
        x2, ..., xm are directly fed to the function.
     2. a cell array like {func, var_pos, fa1, fa2, ... fak}. Here
        func is the function name, function handle or inline object that
        is to be invoked. var_pos is the positions of variable arguments.
        fa1, fa2, ... are the fixed(binded) arguments. If var_pos is [],
        the variable arguments will be put first.

 $ Examples $
   - Invoke a function without fixed arguments
     \{
           slinvoke('plot', x, y);
           y = slinvoke({'sin', []}, x);
     \}

   - Invoke a function with fixed arguments binded, the following pairs
     of statements are equivalent.
     \{
           K = slinvoke({'strfind', 1, 'pattern'}, s);
           K = strfind(s, 'pattern');

           slinvoke({'plot', [1 2], 'r-'}, x, y);
           plot(x, y, 'r-');

           slinvoke({'plot', 2, x, 'b+'}, y);
           plot(x, y, 'b+');

           b = slinvoke({'isequal', [], 0}, x);
           b = isequal(x, 0);
     \}

 $ History $
   - Created by Dahua Lin on Dec 28th, 2005

CROSS-REFERENCE INFORMATION ^

This function calls:
This function is called by:

SOURCE CODE ^

0001 function varargout = slinvoke(invoke_descr, varargin)
0002 %SLINVOKE Invokes a function
0003 %
0004 % $ Syntax $
0005 %   - [y1, y2, ..., yn] = slinvoke(invoke_descr, x1, x2, ..., xm)
0006 %
0007 % $ Arguments $
0008 %   - invoke_descr:       the invoking descriptor
0009 %   - x1, x2, ..., xm:    the input arguments
0010 %   - y1, y2, ..., yn:    the output arguments
0011 %
0012 % $ Description $
0013 %   - [y1, y2, ..., yn] = slinvoke(invoke_descr, placepos, x1, x2, ..., xm)
0014 %     invokes some function with a specified manner. The invoking descriptor
0015 %     can be in following form:
0016 %     1. a string representing the function name, a function handle or an
0017 %        inline object. In this case, no fixed argument is binded. The x1,
0018 %        x2, ..., xm are directly fed to the function.
0019 %     2. a cell array like {func, var_pos, fa1, fa2, ... fak}. Here
0020 %        func is the function name, function handle or inline object that
0021 %        is to be invoked. var_pos is the positions of variable arguments.
0022 %        fa1, fa2, ... are the fixed(binded) arguments. If var_pos is [],
0023 %        the variable arguments will be put first.
0024 %
0025 % $ Examples $
0026 %   - Invoke a function without fixed arguments
0027 %     \{
0028 %           slinvoke('plot', x, y);
0029 %           y = slinvoke({'sin', []}, x);
0030 %     \}
0031 %
0032 %   - Invoke a function with fixed arguments binded, the following pairs
0033 %     of statements are equivalent.
0034 %     \{
0035 %           K = slinvoke({'strfind', 1, 'pattern'}, s);
0036 %           K = strfind(s, 'pattern');
0037 %
0038 %           slinvoke({'plot', [1 2], 'r-'}, x, y);
0039 %           plot(x, y, 'r-');
0040 %
0041 %           slinvoke({'plot', 2, x, 'b+'}, y);
0042 %           plot(x, y, 'b+');
0043 %
0044 %           b = slinvoke({'isequal', [], 0}, x);
0045 %           b = isequal(x, 0);
0046 %     \}
0047 %
0048 % $ History $
0049 %   - Created by Dahua Lin on Dec 28th, 2005
0050 %
0051 
0052 
0053 %% parse input arguments
0054 if ~iscell(invoke_descr)
0055     func = invoke_descr;
0056     var_pos =[];
0057     fix_args = {};
0058 else
0059     len_invoke_descr = length(invoke_descr);
0060     func = invoke_descr{1};
0061     if len_invoke_descr >= 2
0062         var_pos = invoke_descr{2};
0063     else
0064         var_pos = [];
0065     end
0066     if len_invoke_descr >= 3
0067         fix_args = invoke_descr(3:end);
0068     else
0069         fix_args = {};
0070     end
0071 end
0072 
0073 if isempty(var_pos) && ~isempty(varargin)
0074     var_pos = 1:length(varargin);
0075 end
0076 n_fix_args = length(fix_args);
0077 n_var_args = length(var_pos);
0078 if length(varargin) ~= n_var_args
0079     error('sltoolbox:argmismatch', ...
0080         'The variable input arguments do not match that described in invoking descriptor');
0081 end
0082 
0083 
0084 %% organize input arguments
0085 if n_fix_args == 0 && n_var_args == 0
0086     if nargout == 0
0087         feval(func);
0088     else
0089         [varargout{1:nargout}] = feval(func);
0090     end
0091 elseif n_var_args == 0
0092     if nargout == 0
0093         feval(func, fix_args{:});
0094     else
0095         [varargout{1:nargout}] = feval(func, fix_args{:});
0096     end
0097 else
0098     n_args = max(n_fix_args + n_var_args, max(var_pos));
0099     input_args = cell(1, n_args);
0100     indicator_var = false(1, n_args);
0101     indicator_var(var_pos) = true; 
0102     fix_pos = find(~indicator_var);
0103     
0104     for i = 1 : n_var_args
0105         input_args{var_pos(i)} = varargin{i};
0106     end
0107     
0108     for i = 1 : n_fix_args
0109         input_args{fix_pos(i)} = fix_args{i};
0110     end
0111     
0112     if nargout == 0
0113         feval(func, input_args{:});
0114     else
0115         [varargout{1:nargout}] = feval(func, input_args{:});
0116     end
0117 end
0118 
0119 
0120     
0121 
0122 
0123

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com