- #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.
A minor question, why is the size of schools 1 x 4, and not 4 x 1?
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.
versus leaving out the brackets,
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
And got that. So how are these commands outputting the way that they are?
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
Then I did
Code:
B = {schools.SchoolName}
B =
'Cal' 'UCLA' 'Stanford' 'USC'
Last edited: