Bird
Raised Fist0
MATLABdata~10 mins

Structure arrays in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Structure arrays
Define structure fields
↓
Create structure with fields
↓
Create array of structures
↓
Access or modify elements
↓
Use structure array in code
You first define fields, then create structures with those fields, combine them into arrays, and access or change elements as needed.
Execution Sample
MATLAB
person(1).name = 'Alice';
person(1).age = 30;
person(2).name = 'Bob';
person(2).age = 25;
This code creates a structure array 'person' with two elements, each having 'name' and 'age' fields.
Execution Table
StepActionStructure Array StateField ValuesNotes
1Assign person(1).name = 'Alice'person(1)name: 'Alice'First element created with field 'name'
2Assign person(1).age = 30person(1)name: 'Alice', age: 30Added 'age' field to first element
3Assign person(2).name = 'Bob'person(1), person(2)person(1): name='Alice', age=30; person(2): name='Bob'Second element created with 'name'
4Assign person(2).age = 25person(1), person(2)person(1): name='Alice', age=30; person(2): name='Bob', age=25Added 'age' to second element
5Endperson(1), person(2)person(1): name='Alice', age=30; person(2): name='Bob', age=25Structure array fully defined
💡 All elements of the structure array have been assigned their fields.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
personundefinedperson(1).name='Alice'person(1).name='Alice', person(1).age=30person(1), person(2).name='Bob'person(1), person(2).name='Bob', person(2).age=25person(1).name='Alice', person(1).age=30; person(2).name='Bob', person(2).age=25
Key Moments - 3 Insights
Why does MATLAB create the structure array element-by-element instead of all at once?
Because each assignment like person(1).name creates or updates that element, MATLAB builds the structure array step-by-step as shown in steps 1 to 4.
What happens if you assign a field to person(2) before person(1) exists?
MATLAB automatically creates person(1) with empty fields to fill the gap, but this can lead to unexpected empty fields. The execution table shows sequential creation to avoid this.
Can all elements have different fields in a structure array?
No, all elements share the same fields. If a field is missing in one element, MATLAB fills it with empty values. The execution table shows both elements have 'name' and 'age'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of person(1).age after Step 2?
A30
Bundefined
C25
Dempty
💡 Hint
Check the 'Field Values' column at Step 2 in the execution table.
At which step does person(2) first appear in the structure array?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Structure Array State' column to see when person(2) is created.
If you assign person(3).age = 40 without assigning person(3).name, what happens to person(3).name?
AIt copies person(2).name
BIt causes an error
CIt becomes empty
DIt becomes 'unknown'
💡 Hint
Recall that all elements share the same fields; missing fields get empty values.
Concept Snapshot
Structure arrays in MATLAB hold multiple elements with the same fields.
Create elements by assigning fields one by one.
All elements share the same field names.
Access fields with dot notation and index.
Missing fields in elements are empty by default.
Full Transcript
This visual trace shows how MATLAB builds a structure array step-by-step. First, person(1).name is assigned 'Alice', creating the first element. Then person(1).age is added as 30. Next, person(2).name is set to 'Bob', creating the second element, followed by person(2).age as 25. The structure array now has two elements, each with 'name' and 'age' fields. Variables update after each step, and all elements share the same fields. Beginners often wonder why elements are created one at a time and what happens if fields are missing. The quiz questions check understanding of these points.

Practice

(1/5)
1. What is a structure array in MATLAB used for?
easy
A. To perform matrix multiplication
B. To store only numeric arrays
C. To group related data with different types under named fields
D. To create loops for iteration

Solution

  1. Step 1: Understand the purpose of structure arrays

    Structure arrays allow grouping of different types of data under named fields.
  2. Step 2: Compare options with the definition

    Only To group related data with different types under named fields correctly describes this use; others describe unrelated concepts.
  3. Final Answer:

    To group related data with different types under named fields -> Option C
  4. Quick Check:

    Structure arrays = group data by fields [OK]
Hint: Remember: structures group different data types by names [OK]
Common Mistakes:
  • Thinking structures only hold numeric data
  • Confusing structures with loops or matrices
2. Which of the following is the correct way to create a structure array with fields 'Name' and 'Age' in MATLAB?
easy
A. person = struct('Name', 'Alice', 'Age', 30);
B. person = struct(Name = 'Alice', Age = 30);
C. person = struct['Name', 'Alice', 'Age', 30];
D. person = struct{'Name', 'Alice', 'Age', 30};

Solution

  1. Step 1: Recall MATLAB struct syntax

    MATLAB uses struct('field1', value1, 'field2', value2) to create structures.
  2. Step 2: Check each option's syntax

    person = struct('Name', 'Alice', 'Age', 30); uses correct parentheses and quotes. Options B, C, D use invalid syntax.
  3. Final Answer:

    person = struct('Name', 'Alice', 'Age', 30); -> Option A
  4. Quick Check:

    struct uses parentheses and quotes [OK]
Hint: Use parentheses and quotes with struct() [OK]
Common Mistakes:
  • Using square or curly brackets instead of parentheses
  • Omitting quotes around field names
3. What is the output of the following MATLAB code?
student(1).Name = 'John';
student(1).Score = 85;
student(2).Name = 'Jane';
student(2).Score = 92;
disp(student(2).Name);
medium
A. Jane
B. John
C. 85
D. 92

Solution

  1. Step 1: Understand structure array indexing

    student(2) accesses the second element; .Name accesses the 'Name' field.
  2. Step 2: Identify the value stored in student(2).Name

    It is 'Jane' as assigned in the code.
  3. Final Answer:

    Jane -> Option A
  4. Quick Check:

    student(2).Name = 'Jane' [OK]
Hint: Use index and field name to get value [OK]
Common Mistakes:
  • Confusing index 1 and 2
  • Accessing Score instead of Name
4. Identify the error in this MATLAB code snippet:
car.Make = 'Toyota';
car(1).Model = 'Corolla';
car(1).Year = 2020;
medium
A. Field names must be numeric
B. Missing semicolon after first line
C. Mixing scalar and array structure assignment causes error
D. No error, code runs fine

Solution

  1. Step 1: Analyze structure assignment styles

    First line creates scalar structure car with field Make. Next lines assign to car(1), valid for scalar structures.
  2. Step 2: Understand MATLAB structure array rules

    MATLAB allows indexing scalar structures and dynamically adding fields; no error occurs.
  3. Final Answer:

    No error, code runs fine -> Option D
  4. Quick Check:

    Scalar struct indexing + field addition works [OK]
Hint: Scalar structures can be indexed like arrays [OK]
Common Mistakes:
  • Thinking scalar and array assignments cannot mix
  • Believing field names must be numeric
  • Assuming missing semicolons cause errors
5. Given a structure array books with fields Title and Pages, how do you create a new structure array shortBooks containing only books with less than 100 pages?
hard
A. shortBooks = books(books.Pages < 100);
B. shortBooks = books([books.Pages] < 100);
C. shortBooks = books{books.Pages < 100};
D. shortBooks = books(books.Pages > 100);

Solution

  1. Step 1: Extract numeric array of Pages

    Use [books.Pages] to get all Pages values as a numeric array.
  2. Step 2: Use logical indexing to filter

    Compare with < 100 to get logical indices, then index books with these.
  3. Step 3: Check syntax correctness

    shortBooks = books([books.Pages] < 100); correctly uses parentheses and logical indexing; others are incorrect.
  4. Final Answer:

    shortBooks = books([books.Pages] < 100); -> Option B
  5. Quick Check:

    Logical indexing with [books.Pages] works [OK]
Hint: Use [struct.field] for numeric indexing [OK]
Common Mistakes:
  • Using curly braces {} instead of parentheses ()
  • Forgetting to use brackets [] around field extraction
  • Using wrong comparison operator
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard