📖 Scenario: You are managing a small library system. Each book has a title, author, and year of publication. You want to organize this information using structure arrays in MATLAB.
🎯 Goal: Create a structure array to store information about books, add a filter condition to find books published after a certain year, and display the filtered list.
📋 What You'll Learn
Create a structure array called library with fields title, author, and year
Add a variable yearThreshold to filter books published after this year
Use logical indexing to create a new structure array recentBooks with books published after yearThreshold
Display the titles and authors of the filtered books
💡 Why This Matters
🌍 Real World
Structure arrays help organize complex data like records of books, employees, or products in a clear way.
💼 Career
Many jobs in data analysis, engineering, and research use structure arrays to manage and process grouped data efficiently.
Progress0 / 4 steps
1
Create the structure array library
Create a structure array called library with these exact entries: title as 'The Alchemist', '1984', 'To Kill a Mockingbird'; author as 'Paulo Coelho', 'George Orwell', 'Harper Lee'; and year as 1988, 1949, 1960 respectively.
MATLAB
Hint
Use indexing like library(1).title = 'The Alchemist'; to create each book entry.
2
Add the filter variable yearThreshold
Create a variable called yearThreshold and set it to 1970 to filter books published after this year.
MATLAB
Hint
Just write yearThreshold = 1970; on a new line.
3
Filter books published after yearThreshold
Create a new structure array called recentBooks that contains only the books from library where the year field is greater than yearThreshold. Use logical indexing with [library.year].
MATLAB
Hint
Use recentBooks = library([library.year] > yearThreshold); to filter.
4
Display the filtered books
Use a for loop with variables i to iterate over recentBooks and print each book's title and author in the format: Title: [title], Author: [author].
MATLAB
Hint
Use fprintf('Title: %s, Author: %s\n', recentBooks(i).title, recentBooks(i).author); inside a for loop.
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
Step 1: Understand the purpose of structure arrays
Structure arrays allow grouping of different types of data under named fields.
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.
Final Answer:
To group related data with different types under named fields -> Option C
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
Step 1: Recall MATLAB struct syntax
MATLAB uses struct('field1', value1, 'field2', value2) to create structures.
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.
Final Answer:
person = struct('Name', 'Alice', 'Age', 30); -> Option A
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?
C. Mixing scalar and array structure assignment causes error
D. No error, code runs fine
Solution
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.
Step 2: Understand MATLAB structure array rules
MATLAB allows indexing scalar structures and dynamically adding fields; no error occurs.
Final Answer:
No error, code runs fine -> Option D
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
Step 1: Extract numeric array of Pages
Use [books.Pages] to get all Pages values as a numeric array.
Step 2: Use logical indexing to filter
Compare with < 100 to get logical indices, then index books with these.
Step 3: Check syntax correctness
shortBooks = books([books.Pages] < 100); correctly uses parentheses and logical indexing; others are incorrect.
Final Answer:
shortBooks = books([books.Pages] < 100); -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.