Structures help you group related information together in one place. You can easily organize and access data by names instead of numbers.
Structures and field access in MATLAB
Introduction
Syntax
MATLAB
structName.fieldName = value;
Use dot . to access or create fields inside a structure.
You can create a structure by assigning values to fields directly.
Examples
person with fields name and age.MATLAB
person.name = 'Alice'; person.age = 30;
book is a structure holding information about a book.MATLAB
book.title = 'Matlab Basics'; book.author = 'John'; book.year = 2023;
data stores sensor readings.MATLAB
data.temperature = 22.5; data.humidity = 60;
Sample Program
This program creates a structure student with three fields and prints each field's value.
MATLAB
student.name = 'Bob'; student.age = 21; student.grade = 'A'; % Display student information fprintf('Name: %s\n', student.name); fprintf('Age: %d\n', student.age); fprintf('Grade: %s\n', student.grade);
Important Notes
You can add new fields anytime by assigning a value to a new field name.
Use isfield(structure, 'fieldName') to check if a field exists.
Structures can hold different types of data in each field, like numbers, text, or arrays.
Summary
Structures group related data using named fields.
Access fields with dot notation: structure.field.
Structures make your data organized and easy to use.
Practice
1. What is the correct way to access the field
age from a structure person in MATLAB?easy
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 isstructure.field, soperson.ageis correct.Final Answer:
person.age -> Option BQuick 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
Solution
Step 1: Recall MATLAB struct creation syntax
MATLAB uses thestructfunction with pairs of field names and values as strings and values.Step 2: Match correct syntax
car = struct('make', 'Toyota', 'year', 2020); usesstruct('make', 'Toyota', 'year', 2020), which is correct MATLAB syntax.Final Answer:
car = struct('make', 'Toyota', 'year', 2020); -> Option DQuick 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?
person.name = 'Alice'; person.age = 30; disp(person.age);
medium
Solution
Step 1: Understand structure field assignment
The code assigns 'Alice' toperson.nameand 30 toperson.age.Step 2: Identify what
Thedisp(person.age)showsdispfunction prints the value ofperson.age, which is 30.Final Answer:
30 -> Option CQuick Check:
disp shows the value of the accessed field [OK]
Hint: disp(struct.field) prints the field's value [OK]
Common Mistakes:
- Confusing field names with values
- Expecting the field name instead of value
- Syntax errors from missing semicolons
4. Consider this MATLAB code snippet:
data = struct('x', 10, 'y', 20);
disp(data.z);
What will happen when this code runs?medium
Solution
Step 1: Check structure fields
The structuredatahas fieldsxandy, but no fieldz.Step 2: Accessing a non-existent field
Trying to accessdata.zcauses MATLAB to throw an error about the missing field.Final Answer:
Error: Reference to non-existent field 'z'. -> Option AQuick Check:
Accessing missing fields causes an error [OK]
Hint: Access only existing fields; missing fields cause errors [OK]
Common Mistakes:
- Assuming missing fields return 0 or empty
- Ignoring error messages
- Confusing field names with variable names
5. You have a structure array
students with fields name and score. How do you create a new structure passed containing only students with score >= 50?hard
Solution
Step 1: Understand structure arrays and logical indexing
In MATLAB,[students.score]creates an array of scores. Logical indexing selects elements where condition is true.Step 2: Apply logical indexing to filter students
The expressionstudents([students.score] >= 50)selects only students with scores 50 or above.Final Answer:
passed = students([students.score] >= 50); -> Option AQuick Check:
Use logical indexing on structure arrays [OK]
Hint: Use logical indexing with [struct.field] to filter arrays [OK]
Common Mistakes:
- Trying to assign logical array directly
- Using curly braces instead of parentheses
- Creating new struct without filtering
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
