📖 Scenario: You are working on a simple contact list application. Each contact has a name, phone number, and email address. You want to store this information using MATLAB structures.
🎯 Goal: Create a MATLAB structure to hold contact information, add a configuration variable to select a contact, access the fields of the selected contact, and display the contact details.
📋 What You'll Learn
Create a structure array called contacts with exactly three contacts.
Each contact must have fields name, phone, and email with exact values.
Create a variable called selectedIndex to choose which contact to display.
Use field access to get the selected contact's details.
Print the selected contact's name, phone, and email exactly as specified.
💡 Why This Matters
🌍 Real World
Structures are useful to organize related data like contact lists, product catalogs, or student records in MATLAB.
💼 Career
Understanding structures and field access is important for data organization and manipulation in engineering, data analysis, and scientific computing jobs.
Progress0 / 4 steps
1
Create the contacts structure array
Create a structure array called contacts with three contacts. The first contact has name 'Alice', phone '123-456-7890', and email 'alice@example.com'. The second contact has name 'Bob', phone '234-567-8901', and email 'bob@example.com'. The third contact has name 'Charlie', phone '345-678-9012', and email 'charlie@example.com'.
MATLAB
Hint
Use the syntax contacts(index).field = value; to create each contact.
2
Add a selectedIndex variable
Create a variable called selectedIndex and set it to 2 to select the second contact.
MATLAB
Hint
Just write selectedIndex = 2; on a new line.
3
Access the selected contact's fields
Create three variables called selectedName, selectedPhone, and selectedEmail. Assign them the name, phone, and email fields of the contact at index selectedIndex in contacts.
MATLAB
Hint
Use dot notation with contacts(selectedIndex) to get each field.
4
Display the selected contact's details
Write three disp statements to print the selected contact's details exactly as follows: Name: Bob Phone: 234-567-8901 Email: bob@example.com
MATLAB
Hint
Use disp(['Label: ' variable]) to print each line.
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?