MATLAB Mastering Structure Arrays in Matlab: Confusion and Exam Preparation

AI Thread Summary
Confusion around structure arrays in MATLAB often arises from understanding how data is stored and accessed. The size of the structure array "schools" is 1 x 4 because it is defined as a single array containing four elements, rather than four separate arrays. The use of brackets significantly affects the output; for instance, B = [schools.SchoolName] concatenates the strings into a single character array, while B = {schools.SchoolName} creates a cell array that retains individual entries. This distinction highlights the difference between character arrays and cell arrays, where the latter preserves the structure of the data. Familiarity with MATLAB's documentation can clarify these concepts further.
gfd43tg
Gold Member
Messages
947
Reaction score
48
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:
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:
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:
B

B =

CalUCLAStanfordUSC

versus leaving out the brackets,

Code:
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:
 B = {schools.SchoolName}

B = 

    'Cal'    'UCLA'    'Stanford'    'USC'
And got that. So how are these commands outputting the way that they are?
 
Last edited:
Physics news on Phys.org
Maylis said:
A minor question, why is the size of schools 1 x 4, and not 4 x 1?

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."

Maylis said:
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.

Notice here that
Code:
B = schools.SchoolName

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

and that
Code:
B = [schools.SchoolName]

is the same as
Code:
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:
B = ['soup' 'that' 'is' 'hot']
B =

soupthatishot

Maylis said:
Then I did
Code:
 B = {schools.SchoolName}

B = 

    'Cal'    'UCLA'    'Stanford'    'USC'
And got that. So how are these commands outputting the way that they are?

In a cell array the spaces are not ignored, as they specify the different fields. So that's 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:

Similar threads

Back
Top