Be the first to rate this file! 122 downloads (last 30 days) File Size: 2.22 KB File ID: #23125

Multiple assignment

by Miroslav Balda

 

26 Feb 2009

Code covered by BSD License  

The function arg2vars enables to assign more items to a list of variables

Download Now | Watch this File

File Information
Description

The function arg2vars serves for assigning of values from input argument(s) to variables declared in the output list in the place of
the function calling. Not assigned output variables are empty, while superfluous arguments are cut to a number of output variables. Number of allocated items is a minimum out of number of input arguments and number of output variables.

 SYNTAX:
[var1,...,varn] = arg2vars(x)
% x A single input argument. It can be a vector or a matrix of consistent elements, or a cell array of items. Elements of matrices will be stored columnwise.
% [var1,...] List of names of output parameters. If length of the list were greater than the number of items in x, superfluous variables become empty. In turn, only such a number of argument items is used to fill all variables in the list. No errors or warnings are displayed.
[var1,...,varn] = arg2vars(x1,...,xm)
% x1,...,xm list of arguments (scalars, matrices, arrays, texts, etc.)

EXAMPLES:
[a,b,c,d,e,f] = arg2vars(-1.234, 'abcde', [1,exp(1),pi], 1:5, eye(3))
% a = -1.2340
% b = abcde
% c = 1.0000 2.7183 3.1416
% d = 1 2 3 4 5
% e = 1 0 0
% 0 1 0
% 0 0 1
% f = []

[a,b,c,d] = arg2vars({'ABC','DEFG','HIJK'})
% a=A, b=B, c=C, d=D, e=E, f=F
[a,b,c] = arg2vars({{'ABC'},{'DEFG'}})
% a = [{1x1 cell}, {1x1 cell}]
% b = [], c = []
% a{:}(2) = 'DEFG', a{:}{2} = DEFG

[a,b,c] = arg2vars({{'ABC'}},{{'DEFG'}})
% a = {1x1 cell}, a{:} = 'ABC'
% b = {1x1 cell}, b{:} = 'DEFG'
% c = []

[a,b,c] = arg2vars(zeros(1,5),pi,{{'a','bcd','e'}},0);
% a = 0 0 0 0 0
% b = 3.1415926...
% c{:} = 'a' 'bcd' 'e'

[E,b,c,sfa,efa,na,Ka,Nc,sc,w,ec,Nb,t,sigv,DL,D,Nz,ssq,flag,figh] = arg2vars({repmat([],1,30)});
% Defines all variables empty

MATLAB release MATLAB 7.3 (R2006b)
Other requirements Windows.
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
27 Feb 2009 Jos (10584)

Most of this behavior can be handled by DEAL, for instance, a,b,c] = deal([])
(btw, the later example is rather strange as I would expect
"[E,b,c...] = arg2vars" to do the job ...)

The need for putting cell arrays into a cell to assign it to a single variable is in my opinion very contra-intuitive given the fact that a regular array is not split into separate variables.

To me, the only advantage of this submission seems to be that unassigned output arguments are set to empty, but a simple wrapper function around deal will do the same:

function [varargout] = mydeal(varargin) ;

varargout([1 2:nargout]) = {[]} ; % return [] when called without any in/output arguments
if nargin,
    N = max(min(nargin,nargout), 1) ;
    % how many things do we have to be DEALt with?
    [varargout{1:N}] = deal(varargin{1:N}) ;
end

28 Feb 2009 Miroslav Balda

I am sorry that I have not known that the function deal exist. I always learn something new. BTW, the last example may be simplified on the right hand side of assignment by arg2vars([]).

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
general Miroslav Balda 26 Feb 2009 13:25:23
programming Miroslav Balda 26 Feb 2009 13:25:23
data handling Miroslav Balda 26 Feb 2009 13:25:23
 

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