Code covered by the BSD License  

Highlights from
unpack

Be the first to rate this file! 0 Downloads (last 30 days) File Size: 1.44 KB File ID: #15162

unpack

by M MA

 

31 May 2007 (Updated 01 Jun 2007)

Unpack list to variables

| Watch this File

File Information
Description

Many languages allow list unpack as:
a,b,c=1,2,'w'

This cannot be done in matlab and can avoid many lines of code. This file makes it possible.

Examples:
[a,b,c,d]=unpack(1,2,3,'hello');
[a,b,c]=unpack([1,2,3]);
[a,b,c,d]=unpack({1,2,3,'hello'});

(I suppose this works in any matlab release)

MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
01 Jun 2007 urs (us) schwarz

could be a nice snippet...
however, idiosyncratic behavior - or insufficient help, rather - prevents it from becoming a 5-star contender, yet
% 1) this works as expected/advertised
     [a,b,c,d]=unpack({1,2,3,4})
% ans = 4 double values
% 2) this does not and yields an error
     [a,b,c,d]=unpack({1,2},{3,4})
% -and- the syntax now must be
     [a,b]=unpack({1,2},{3,4})
% ans = 2 cells
% which is not immediately clear from the
% help section

furthermore, a pointer to DEAL should be included...

us

01 Jun 2007 Jos x@y.z

I fail to see the advantage over DEAL ...

[a,b,c,d] = deal(1,2,3,'hello')
A = {1,'xx',zeros(3)}
[x,y,z] = deal(A{:})

or am I missing something

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
unpack M MA 22 Oct 2008 09:14:06
list M MA 22 Oct 2008 09:14:06
variables M MA 22 Oct 2008 09:14:06
allow M MA 22 Oct 2008 09:14:06
utilities M MA 22 Oct 2008 09:14:06

Contact us at files@mathworks.com