Bird
Raised Fist0
MATLABdata~20 mins

Structure arrays in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Structure Arrays Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of accessing a field in a structure array?

Consider the following MATLAB code:

students(1).name = 'Alice';
students(1).age = 21;
students(2).name = 'Bob';
students(2).age = 22;

result = {students.name};

What is the value of result after running this code?

MATLAB
students(1).name = 'Alice';
students(1).age = 21;
students(2).name = 'Bob';
students(2).age = 22;

result = {students.name};
A{'Alice'; 'Bob'}
B['Alice', 'Bob']
C['Alice'; 'Bob']
D{'Alice', 'Bob'}
Attempts:
2 left
💡 Hint

Remember that curly braces {} create cell arrays in MATLAB.

❓ Predict Output
intermediate
2:00remaining
What is the size of a structure array after assignment?

Given this MATLAB code:

data(3).value = 0;
data(1).value = 10;
data(2).value = 20;

What is the size of data after these assignments?

MATLAB
data(3).value = 0;
data(1).value = 10;
data(2).value = 20;
A3x1 structure array
B1x2 structure array
C1x3 structure array
D3x3 structure array
Attempts:
2 left
💡 Hint

Assigning to data(3) creates elements 1 to 3 in a row vector by default.

🔧 Debug
advanced
2:00remaining
Why does this code produce an error when accessing a field?

Examine this MATLAB code:

info(1).name = 'John';
info(2).age = 30;

names = {info.name};

What error will occur when running this code?

MATLAB
info(1).name = 'John';
info(2).age = 30;
names = {info.name};
AField 'name' does not exist for index 2.
BSyntax error: missing semicolon.
Cnames is an empty cell array {}.
DNo error; names = {'John', ''}.
Attempts:
2 left
💡 Hint

Check if all elements of the structure array have the field name.

🧠 Conceptual
advanced
2:00remaining
How to add a new field to all elements of a structure array?

You have a structure array records with 5 elements. You want to add a new field status with value 'active' to every element. Which code does this correctly?

Arecords.status = 'active';
B
for i = 1:length(records)
  records(i).status = 'active';
end
Crecords(:).status = 'active';
Drecords.status(:) = 'active';
Attempts:
2 left
💡 Hint

Think about how MATLAB assigns fields to structure arrays element-wise.

❓ Predict Output
expert
2:00remaining
What is the output of this nested structure array code?

Consider this MATLAB code:

team(1).player.name = 'Anna';
team(1).player.score = 10;
team(2).player.name = 'Ben';
team(2).player.score = 15;

scores = [team.player.score];

What is the value of scores after running this code?

MATLAB
team(1).player.name = 'Anna';
team(1).player.score = 10;
team(2).player.name = 'Ben';
team(2).player.score = 15;

scores = [team.player.score];
A[10 15]
B[[10] [15]]
C[10; 15]
DError: Cannot concatenate nested fields directly.
Attempts:
2 left
💡 Hint

Accessing nested fields in structure arrays returns arrays of those values.

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