A structure in MATLAB is a data type that groups related data using named fields, allowing you to organize different types of data together like a real-world object.
Click to reveal answer
beginner
How do you create a structure with fields 'Name' and 'Age' in MATLAB?
You can create it like this: person.Name = 'Alice'; person.Age = 30;
Click to reveal answer
beginner
How do you access the field 'Age' from a structure variable 'person'?
Use dot notation: person.Age returns the value stored in the 'Age' field.
Click to reveal answer
intermediate
How can you check if a field exists in a structure?
Use the function isfield(structure, 'fieldname'). It returns true if the field exists, false otherwise.
Click to reveal answer
intermediate
What happens if you try to access a field that does not exist in a MATLAB structure?
MATLAB gives an error saying the field does not exist. You should check with isfield before accessing to avoid this.
Click to reveal answer
How do you add a new field 'Height' with value 170 to an existing structure 'person'?
Aperson.Height = 170;
Baddfield(person, 'Height', 170);
Cperson('Height') = 170;
Dperson->Height = 170;
✗ Incorrect
In MATLAB, you add or modify fields using dot notation like person.Height = 170;.
Which function checks if a field exists in a structure?
Afieldexists()
Bhasfield()
Cisfield()
Dcheckfield()
✗ Incorrect
The correct function is isfield(structure, 'fieldname').
What is the output of person.Name if person.Name = 'Bob';?
AName
BError
Cperson.Name
D'Bob'
✗ Incorrect
Accessing person.Name returns the string stored in that field, here 'Bob'.
How do you create an empty structure with fields 'X' and 'Y'?
Astruct(X, Y)
Bstruct('X', [], 'Y', [])
Cemptystruct('X', 'Y')
Dnewstruct('X', 'Y')
✗ Incorrect
Use struct('X', [], 'Y', []) to create a structure with empty fields.
What happens if you try to access person.Weight but 'Weight' is not a field?
AThrows an error
BReturns empty
CReturns zero
DCreates the field automatically
✗ Incorrect
MATLAB throws an error if you access a non-existent field.
Explain how to create a MATLAB structure and access its fields.
Think about how you name fields and get their values.
You got /3 concepts.
Describe how to safely check if a field exists before accessing it in a structure.
Use a function that returns true or false for field presence.
You got /3 concepts.
Practice
(1/5)
1. What is the correct way to access the field age from a structure person in MATLAB?
easy
A. person['age']
B. person.age
C. person->age
D. person.age()
Solution
Step 1: Understand MATLAB structure field access
In MATLAB, fields of a structure are accessed using dot notation without parentheses or brackets.
Step 2: Identify correct syntax for field access
The correct syntax is structure.field, so person.age is correct.
Final Answer:
person.age -> Option B
Quick Check:
Use dot notation for fields [OK]
Hint: Use dot notation to access structure fields in MATLAB [OK]
Common Mistakes:
Using parentheses like a function call
Using brackets like a dictionary
Using arrow notation from other languages
2. Which of the following is the correct way to create a structure car with fields make and year in MATLAB?
easy
A. car = struct(make = 'Toyota', year = 2020);
B. car = {make: 'Toyota', year: 2020};
C. car = struct['make' => 'Toyota', 'year' => 2020];
D. car = struct('make', 'Toyota', 'year', 2020);
Solution
Step 1: Recall MATLAB struct creation syntax
MATLAB uses the struct function with pairs of field names and values as strings and values.
Step 2: Match correct syntax
car = struct('make', 'Toyota', 'year', 2020); uses struct('make', 'Toyota', 'year', 2020), which is correct MATLAB syntax.
Final Answer:
car = struct('make', 'Toyota', 'year', 2020); -> Option D
Quick Check:
Use struct('field', value, ...) to create structures [OK]
Hint: Use struct('field', value) pairs to create structures [OK]
Common Mistakes:
Using JSON or dictionary syntax
Using equal signs inside struct()
Using brackets instead of parentheses
3. What is the output of the following MATLAB code?