Mastering Structure Arrays in Matlab: Confusion and Exam Preparation

In summary: Essentially, the brackets tell MATLAB to concatenate the strings, while the curly brackets tell it to keep them separate as individual elements in a cell array.In summary, Structure arrays in MATLAB can be a bit confusing at first, but reading through the documentation can help clarify any misunderstandings. The size of a structure array is determined by the way the values are specified, and using brackets or curly brackets can result in different outputs. Brackets concatenate the strings, while curly brackets keep them separate as individual elements in a cell array.
  • #1
gfd43tg
Gold Member
950
50
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
  • #2
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:

1. What are structure arrays in Matlab?

Structure arrays in Matlab are a way to store and organize data in a hierarchical structure. They allow you to group related data together and access it using field names.

2. How do I create a structure array in Matlab?

To create a structure array in Matlab, you can use the struct function. You can also use the fieldnames function to define the field names and then use the dot notation to assign values to those fields.

3. How do I access data in a structure array in Matlab?

You can access data in a structure array using the dot notation. For example, if you have a structure array called student with a field name, you can access the name of the first student by using student(1).name.

4. What are some common sources of confusion when working with structure arrays in Matlab?

Some common sources of confusion when working with structure arrays in Matlab include not understanding how to access data using field names, not properly initializing the structure array, and not understanding the difference between structure arrays and cell arrays.

5. How can I use structure arrays to prepare for exams in Matlab?

You can use structure arrays to organize and store data for studying and practicing for exams in Matlab. By grouping related data together, you can easily access and review specific information. Additionally, you can use structure arrays to create practice problems and test your understanding of accessing and manipulating data.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
Back
Top