0
0
MATLABdata~30 mins

Structure arrays in MATLAB - Mini Project: Build & Apply

Choose your learning style9 modes available
Structure Arrays
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use fprintf('Title: %s, Author: %s\n', recentBooks(i).title, recentBooks(i).author); inside a for loop.