Mastering Structure Arrays in Matlab: Confusion and Exam Preparation

Click For Summary
SUMMARY

This discussion focuses on the intricacies of structure arrays in MATLAB, specifically addressing the confusion surrounding the output of commands involving structure fields. The user illustrates their queries using an example structure array named 'schools', which contains school names and team names. Key points include the distinction between using brackets and not using them, which affects the output format and size of the resulting variables. The user learns that 'B = [schools.SchoolName]' produces a single character string, while 'B = {schools.SchoolName}' results in a cell array containing individual school names.

PREREQUISITES
  • Understanding of MATLAB structure arrays
  • Familiarity with MATLAB cell arrays
  • Knowledge of MATLAB indexing and data types
  • Basic command line operations in MATLAB
NEXT STEPS
  • Review MATLAB documentation on structure arrays
  • Learn about the differences between character arrays and cell arrays in MATLAB
  • Explore MATLAB indexing techniques for accessing structure fields
  • Practice creating and manipulating structure arrays with various data types
USEFUL FOR

Students preparing for MATLAB exams, educators teaching MATLAB concepts, and anyone looking to deepen their understanding of structure arrays and their manipulation in MATLAB.

gfd43tg
Gold Member
Messages
949
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

Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
9K
Replies
5
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
15
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K