Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part I: Creating and Searching a Vertical String Array (10pts) Matlab Question I

ID: 3768238 • Letter: P

Question

Part I: Creating and Searching a Vertical String Array (10pts)

Matlab Question

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.

Explanation / Answer

I hope this is what you were looking for.


flag = true;
while flag
    nos = input('How many strings do you want to enter? ');
    if(nos <= 0)
        disp('Enter a poitive number.');
    else
        flag = false;
    end
end

n = floor(nos);

for i=1:n
    str = input('Enter the string: ','s');
    if(i == 1)
        matrix = strvcat(str);
    else
        matrix = strvcat(matrix, str);
    end
end

cell_matrix = cellstr(matrix);

fstr = input('Enter the string you want to find in the matrix: ','s');

tf = strcmp(cell_matrix, fstr);

flag = false;
for i=1:n
    if(tf(i) == 1)
        disp([fstr, ' found at row number ', num2str(i)]);
        flag = true;
    end
end

if(flag == false)
    disp(['Error: ', fstr, ' not found'])
end
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote