In MATLAB Part I: Creating and Searching a Vertical String Array Introduction Of
ID: 3772095 • Letter: I
Question
In MATLAB
Part I: Creating and Searching a Vertical String Array
Introduction
Often we want to gather some data, store it, and then later search for it. This data may be numeric or non-numeric. We’ve already looked at searching for numerical data in a vector. If our data is string data we can store it in a matrix using strvcat and then check if the desired string is any of the rows if this matrix.
Todo
First ask the user for how many strings they plan to enter. Keep asking them until they enter a positive number (floor it to make it an integer). Let’s call this number n
Then ask the user to enter n strings, each time adding it as a new row to the matrix storing the data. Use the Matlab function strvcat to do this.
After the user has entered n strings ask them for a string to look for in the string matrix. If you find the string in any of the rows return that row number. Otherwise return an error message.
Hints
• Let us assume you are storing the data in a variable called data. For the examples that follow your data variable will grow as follows:
data=[] data=[‘Look for me’];
data=[‘Look for me’; ‘CS105 ’ ];
data=[‘Look for me’;’CS105 ’; ‘Test today?’ ];
• Remember how to compare strings?
• When doing strvcat extra space is added as needed (see Note above). So when you compare you should probably first strip any extra space. You do this with the strtrim function
• You have to check each row in data. Do you remember how to get the number of rows from a matrix?
• Remember how to get all the data for a single row of a matrix?
Explanation / Answer
Sample code:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.