Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

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

4 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{:};

Leave a Reply


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

  • Doug: Gamal, That is something that can be done. Do you have the Data Acquisition toolbox? What have you tried, can...
  • Gamal: Dear Sir, I am working in a project that i use an Analogue to digital converter HW to transfer analogue...
  • Berdakh Abibullaev: Great! This is very convinient to use. But I use matlab codes for opening the data and...
  • jiro: Mahmood, It’s probably best to ask the author of the tool for specific questions regarding the...
  • Mahmood: Hi there, when I run this program application, it gives me the following error ??? Error using ==>...
  • Brett: Hi M, Back from vacation…just downloaded DEMORSE, and ran it on the WAV file I created for this post....
  • Quan Quach: Doug is my favorite MathWorks blogger. Loren is also my favorite. That Mike guy not so bad. So Arthur got...
  • Doug: Maram, You might want to look at the Database toolbox. http://www.mathworks .com/products/databa se/ Doug
  • Bob: Nayaki, if you need additional help using a File Exchange submission, you might try contacting the author. You...
  • maram: hi, I have a question I have a matlab code and i want to read a data from SQL server database to use it in...

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

Related Topics