0
0
MATLABdata~20 mins

Structure arrays in MATLAB - Practice Problems & Coding Challenges

Choose your learning style9 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.