Be the first to rate this file! 24 downloads (last 30 days) File Size: 899 Bytes File ID: #23254

Map fields of a structure to output variables

by Hoi Wong

 

11 Mar 2009 (Updated 30 Mar 2009)

No BSD License  

The program assigns each field of a structure into output arguments.

Download Now | Watch this File

File Information
Description

This is made in response to one of the comments in:
http://blogs.mathworks.com/loren/2008/01/24/deal-or-no-deal/

The program assigns each field of a structure into output arguments.

    s =
        a: 1
        b: [1 2 4]
        c: [5x2 double]
        d: 'test'
        e: {[3]}

    >> [a b c d e]=struct2vars(s)

    a =
         1

    b =
         1 2 4

    c =
        0.8147 0.0975
        0.9058 0.2785
        0.1270 0.5469
        0.9134 0.9575
        0.6324 0.9649

    d =
    test

    e =
        [3]

A even better usage if you're trying to dump all your struct into the current workspace:
T=fieldnames(S);
[T{:}]=struct2vars(S);

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
12 Mar 2009 Jos (10584)

A superfluous combination of two basic ML commands. Simply using
C = struct2cell(S) ; [a,b,c] = deal(C{:}) ; in your code is clear enough.

15 Mar 2009 Hoi Wong

Thanks jos for the alternative approach that use deal(). The reason that I made this wrapper is to avoid using deal() and make the code self-documenting.

I wish Mathworks can consider allowing syntaxes like [a,b,c]=(struct2cell(S)){:}, then my submission has no reason to be here. :)

Please login to add a comment or rating.
Updates
11 Mar 2009

Modified tag to be more sensible. I mistakenly tagged it with a URL

30 Mar 2009

Added this usage case in comments file:

T=fieldnames(S);
[T{:}]=struct2vars(S);

Tag Activity for this File
Tag Applied By Date/Time
variables Hoi Wong 12 Mar 2009 13:42:48
struct Hoi Wong 12 Mar 2009 13:42:48
varargout Hoi Wong 12 Mar 2009 13:42:48
argument Hoi Wong 12 Mar 2009 13:42:48
data structure Hoi Wong 12 Mar 2009 13:42:48
workspace Hoi Wong 30 Mar 2009 15:58:11
variable Hoi Wong 30 Mar 2009 15:58:11
dump Hoi Wong 30 Mar 2009 15:58:11
 

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