Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Structure Arrays (matlab)

Tags:
  1. Jul 30, 2014 #1

    Maylis

    User Avatar
    Gold Member

    Hello,

    I am preparing for the exam for my Matlab class, and have run into some confusion with Structure arrays. I have a hard time keeping all the concepts straight in my head. I am working on an old exam problem, where this is the structure array, which I will use as an example to demonstrate my misunderstandings.

    Code (Text):
    schools(1).SchoolName = 'Cal';
    schools(1).TeamName = 'Golden Bears';
    schools(2).SchoolName = 'UCLA';
    schools(2).TeamName = 'Bruins';
    schools(3).SchoolName = 'Stanford';
    schools(3).TeamName = 'Cardinals';
    schools(4).SchoolName = 'USC';
    schools(4).TeamName = 'Trojans';
    A minor question, why is the size of schools 1 x 4, and not 4 x 1?

    Code (Text):
    B = [schools.SchoolName];
    C = {schools.SchoolName};
    The next couple of questions threw me off. It was asking for the size of B. I noticed the brackets made a huge difference here.
    Code (Text):
    B

    B =

    CalUCLAStanfordUSC
    versus leaving out the brackets,

    Code (Text):
    B = schools.SchoolName

    B =

    Cal
    Why does this difference occur just because of brackets? To me, it's sort of like typing [3] versus 3 in the command window, they would yield the same thing. But in this case, they clearly do not.

    Then I did
    Code (Text):
     B = {schools.SchoolName}

    B =

        'Cal'    'UCLA'    'Stanford'    'USC'
    And got that. So how are these commands outputting the way that they are?
     
    Last edited: Jul 30, 2014
  2. jcsd
  3. Jul 31, 2014 #2

    kreil

    User Avatar
    Gold Member

    First, many of the questions you're asking are answered in the documentation. So I recommend you give that a solid read.

    http://www.mathworks.com/help/matlab/ref/struct.html

    In this case, the size of the structure has to do with how you specify the values:

    "If none of the value inputs is a cell array, or all of the value inputs are scalar cell arrays, then output s is a scalar structure. Otherwise, value inputs that are nonscalar cell arrays must have the same dimensions, and output s also has those dimensions. For any value that is a scalar cell array or an array of any other data type, struct inserts the contents of value in the relevant field for all elements of s."

    Notice here that
    Code (Text):

    B = schools.SchoolName
     
    is the same as
    Code (Text):

    B = schools(1).SchoolName
     
    and that
    Code (Text):

    B = [schools.SchoolName]
     
    is the same as
    Code (Text):
     
    B = [schools(1).SchoolName schools(2).SchoolName schools(3).SchoolName schools(4).SchoolName]
     
    This is a character string, and the empty spaces are ignored, so B becomes a single long character string of size 1x18 (it has 18 characters). It's the same as,

    Code (Text):

    B = ['soup' 'that' 'is' 'hot']
    B =

    soupthatishot
     
    In a cell array the spaces are not ignored, as they specify the different fields. So thats what you get, a cell array with 4 different fields. So the difference between B and C boils down to the difference between a cell array of strings and a character array.
     
    Last edited: Jul 31, 2014
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Structure Arrays (matlab)
  1. Matlab and arrays (Replies: 0)

  2. MatLab structures (Replies: 5)

  3. Arrays in MATLAB (Replies: 3)

Loading...