File Exchange Pick of the Week

April 22nd, 2008

MATLAB Basics: Array of structures vs Structures of arrays

This short video covers the difference between a “structure of arrays” and an “array of structures”.

Video Content

iconFiles.jpgiconPod.jpg

7 Responses to “MATLAB Basics: Array of structures vs Structures of arrays”

  1. Emil replied on :

    It’s easier to create vector s by:
    s = [people.SimmNum];

  2. Tom replied on :

    This and Emil’s comment were quite helpful to me.

    To take this one step further, one advantage of Arrays of Structures would be the ability to have vectors of different lengths for each numerical part, and a cell array of names of different lengths for each name part.

    For example:

    people(1).name=’Erin’
    people(2).name={’Joan’ ‘Carla’}
    people(1).sim=44
    people(2).sim=[561 49]

  3. Doug replied on :

    Emil,

    Yes, you are correct. I can not believe I over looked that. There are so many ways to do certain things in MATLAB.

    Doug

  4. Ryan Gray replied on :

    If you have an array that you want to assign to a field across a structure array, you can do this:

    [structArray(:).fieldname] = deal(num2cell(array))

    Of course, the length of array should be the same as the length of structArray, but if structArray doesn’t exist yet, then you can do:

    [structArray(1:length(array)).fieldname] = deal(num2cell(array))

    If array is already a cell array, you can avoid using both deal and num2cell (although they will still work) and use:

    [structArray(:).fieldname] = array{:}
    or
    [structArray(1:length(array)).fieldname] = array{:}

    An example, modifying the struct array ‘people’ from the video:

    names = {’Larry’,'Moe’,'Curly’};
    [people(:).name] = names{:};

  5. liuzhi replied on :

    In one of my program, I’d like to use an array of array of structures, such like

    s0 = struct('field1', [v1 v2], 'field2', [v3 v4]);
    s1 = struct('field1', [v1' v2'], 'field2', [v3' v4']);
    

    I want another data structure s = [s0, s1] so that I can access s0(1) like s(0,1). How can I do that?

  6. Doug replied on :

    You want to use a cell array here:

    
    s{1} = s0
    s{2} = s1
    s{1}.field1
    s{1}.field1(1,3)
    

    Doug

  7. Dan replied on :

    I may be wrong, but one of the examples in Post 4 (Ryan Gray) doesn’t work as advertised. Specifically,

    [structArray(1:length(array)).fieldname] = deal(num2cell(array))
    

    ends up assigning the whole array to every instance of fieldname, instead of distributing the array across those instances. What does seem to work is:

    cell_array = num2cell(array);
    [structArray(1:length(array)).fieldname] = cell_array{:};
    

    I leave it as an exercise for a MATLAB guru to splain why this is the case.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • Zach: Hi Doug and Les, I didn’t have a lot of time to mess with this, but I did find a work-around. I plotted...
  • hamed: k
  • Les: @Zach This isn’t exactly what you are looking for but at least it puts all three parameters on the same...
  • Zach: Thanks for your suggestions Doug. I’ll give that a shot and see what happens. I’ve seen many of...
  • Doug: @Zach, I would say to use plotYYY, because that is close to what you want, but using depth as Y makes sense....
  • Doug: @Teja, I think this will work: http://www.mathworks .com/access/helpdesk /help/techdoc/ref...
  • Gify: merry christmas :) nice christmas tree! Regards, Janet Gify
  • Teja: Dear Doug Is there anyway to plot a surface from nonuniform data without meshgrid and griddata? Basically i...
  • Zach: I’m working with geophysical data, so I’d like to produce a depth profile. The y-axis would be...
  • Doug: @Ashok First, please do not use variable names that are MATLAB commands (std and mean). Second, p(j) should be...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.