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.

  • oleg: The author has implemented skewness, kurtosis and checks answering appropriately to the critic.
  • Ashok: how to store 10 or more random number in a loop a loop for i = 1:n mean(i) = input(’enter the mean value...
  • Ben: Doug, Thanks for the very helpful videos! Uitables seem like a convenient way to make a customized property...
  • oleg: Allstats has no checks, no comments and could also be improved (talking about prctile implementatio). Not to...
  • Todd: Additional information and a link to download free MATLAB and Simulink LEGO MINDSTORMS NXT code can be found at...
  • Doug: @Leo, Here is the “English version” of that code. “vec = []” makes an empty variable...
  • leo: Dear Doug I have a question in your code ‘October 9th, 2009 at 13:53′ vec = []; vec = [vec val]; I...
  • Shanker Keshavdas: You sir, are a gentleman and a scholar… No really, helped me out a lot. As to what is...
  • Quan Zheng: how can I get a copy of stepspecs.m?
  • Doug: @Lucy I think this is what you seek to move a line with the mouse in MATLAB. http://blogs.math...

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