File Exchange

image thumbnail

Map fields of a structure to output variables

version 1.2 (899 Bytes) by

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

0 Downloads

Updated

No License

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);

Comments and Ratings (2)

Hoi Wong

Hoi Wong (view profile)

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. :)

Jos (10584)

Jos (10584) (view profile)

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

Updates

1.2

Added this usage case in comments file:

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

1.1

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

MATLAB Release
MATLAB 7.5 (R2007b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today