0
0
MATLABdata~30 mins

String searching (contains, strfind) in MATLAB - Mini Project: Build & Apply

Choose your learning style9 modes available
String Searching with contains and strfind in MATLAB
📖 Scenario: You work in a library and need to find books that mention certain keywords in their titles.
🎯 Goal: Build a MATLAB program that searches for a keyword in a list of book titles using contains and strfind.
📋 What You'll Learn
Create a cell array of book titles
Create a keyword variable to search for
Use contains to find which titles include the keyword
Use strfind to find the position of the keyword in each title
Display the results clearly
💡 Why This Matters
🌍 Real World
Searching for keywords in book titles helps librarians quickly find relevant books for readers.
💼 Career
Knowing how to search strings is useful in data analysis, text processing, and software development.
Progress0 / 4 steps
1
Create a list of book titles
Create a cell array called bookTitles with these exact strings: 'Matlab Basics', 'Advanced Programming', 'Data Science', 'Machine Learning', and 'Deep Learning'.
MATLAB
Need a hint?

Use curly braces {} to create a cell array of strings in MATLAB.

2
Set the keyword to search
Create a variable called keyword and set it to the string 'Learning'.
MATLAB
Need a hint?

Assign the string 'Learning' to the variable keyword.

3
Find titles containing the keyword using contains and strfind
Use contains with bookTitles and keyword to create a logical array called hasKeyword. Then use a for loop with index i from 1 to length of bookTitles to find the position of keyword in each title using strfind. Store the result in a cell array called positions.
MATLAB
Need a hint?

contains returns a logical array. Use strfind inside a loop to get positions.

4
Display the search results
Use a for loop with index i from 1 to length of bookTitles to print each title. If hasKeyword(i) is true, print 'Found at position(s):' followed by the positions from positions{i}. Otherwise, print 'Keyword not found'.
MATLAB
Need a hint?

Use fprintf to print each title and the search results. Use mat2str to convert positions to string.