Code covered by the BSD License  

Highlights from
deal2 - Multiple assignment

deal2 - Multiple assignment

by

 

16 Aug 2011 (Updated )

The function deal2 is an extension of the function deal.

deal2(varargin)
function varargout = deal2(varargin)
%   DEAL2    Transfer input parameters to output
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The function deal2 is designated for multiple assignment. It is an
% extension of the function deal. The main differences between both
% functions are:
% 
%   - deal2 need not have equal number of input and output arguments,
%   - deal2 makes all output arguments empty for missing input arguments,
%   - deal2 transfers at most nargout arguments from input arguments,
%   - deal2 may split input arguments into elements and tranfer them to
%           output arguments.
%   - deal2 may decompose matrices into columns and store them in output
%           arguments
%
% All superfluous output arguments are made empty, and all superfluous
% input arguments are not transfered.
%
% SYNTAX:
%
% [arg1,...,argn] = deal2;                          %   arg(k) = []
% [arg1,...,argn] = deal2(item);                    %   arg(k) = item
% [arg1,...,argn] = deal2(list of items);           %   arg(k) = item(k)   
% [arg1,...,argn] = deal2('_elm_',list of items);   % arg(k) = element(k)
% [arg1,...,argn] = deal2('_mtx_',list of items);   % arg(k) = column(k)
%
% EXAMLES:
%       Make otput arguments empty:
% [a,b,c,d,e,f,g] = deal2
%    a=[],  b=[],  c=[],  d=[],  e=[],  f=[],  g=[]
%
%       Clear output arguments:
% [a,b,c,d,e,f,g] = deal2(0)
%    a=0,  b=0,  c=0,  d=0,  e=0,  f=0,  g=0
%
%       Similar to the function deal (but the number of arguments):
% [a,b,c,d,e,f,g] = deal2('','XYZ',1:3,{'STUV'})
%    a='',  b=XYZ,  c=[1 2 3],  d='STUV',  e=[],  f=[],  g=[]
%
%       Split input parameters into elements and send them to output:
% [a,b,c,d,e,f,g,h] = deal2('_elm_','','XYZ',1:3,{'STUV'})
%    a='', b=X,  c=Y,  d=Z,   e=1,  f=2,  g=3, h='STUv'
% [a,b,c,d,e,f,g] = deal2('_elm_',{'','XYZ',1:3,{'STUV'}})
%    a={''},  b='XYZ',  c=[1x3 double],  d={1x1 cell},  e=[],  f=[],  g=[]
%                       c{:}=[1 2 3],    d{:}='STUV'
% [a,b,c,d,e,f] = deal2('_elm_',pi,{1,'A',2; 3,'B',4})
%    a=3.1416,  b=[1],  c=[3],  d='A',  e='B',  f=[2]

% Miroslav Balda
% miroslav@balda.cz
% 2010-05-20    v 1.0   extended functionality compared to deal | arg2vars
% 2011-07-15    v 1.1   enables empty items
% 2011-10-01    v 1.2   decomposes matrices into columns
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

varargout = cell(1,nargout);    %   Make all varargouts empty
narg = min(nargin,nargout);
if narg<=0, return, end
                                %   Transfer varargin to varargout
if length(varargin)==1
    varargout(:) = deal(varargin);      %   single input argument
else                                    %   multiple input arguments
    v1 = varargin{1};                   %   first input argument
    if ischar(v1) && (strcmpi(v1,'_elm_') || ...
                      strcmpi(v1,'_mtx_'))  %   decompose input arguments
        k = 0;
        for j = 2:nargin
            vj = varargin{j};
            if numel(vj)
                if strcmpi(v1,'_elm_')
                    for l = 1:numel(vj)     %   nonempty an-or row vector
                        k = k+1;
                        if k>nargout, return, end
                        varargout(k) = {vj(l)};     %   l-th element of vj
                    end % l
                else                        %   Matrix
                    for l = 1:size(vj,2)
                        k= k+1;
                        if k>nargout, return, end
                        varargout(k) = {vj(:,l)};   %   l-th column of vj
                    end
                end % if
            else                            %   empty element
                k=k+1;
                varargout(k) = {vj};
            end % if
        end % j
    else                                    %   general multiple assignment
        varargout(1:narg) = deal(varargin(1:narg)); 
    end % if
end % if

Contact us