Borrar filtros
Borrar filtros

Searching a cell array for a string

1 visualización (últimos 30 días)
Bob Sherland
Bob Sherland el 26 de Abr. de 2018
Respondida: Walter Roberson el 26 de Abr. de 2018
I have the following code, which is supposed to go through the cell array books and search for the search title, and then show the information in the whole row of the cell, so since the search in this case is 'book three' so book should = 'Book Three' 'Author two' [122], it isnt working and im wondering where I have gone wrong and how to fix it.
books={'Book one' 'Author one' [122];'Book two' 'Author two' [122];'Book Three' 'Author two' [122]};
searchtitle='Book Three';
rowscolumns=size(books);
rows=rowscolumns(1,1);
rowcount=1;
book={};
for i = 1:rows
find_book=books(rowcount,1);
find_book=lower(find_book);
find_book={find_book};
if isequal(find_book, searchtitle)
book=books(rowcount,:);
end
rowcount=rowcount+1;
end
disp(book)

Respuestas (1)

Walter Roberson
Walter Roberson el 26 de Abr. de 2018
You lower() the books entry but not searchtitle.
You can do better anyhow:
books={'Book one' 'Author one' [122];'Book two' 'Author two' [122];'Book Three' 'Author two' [122]};
searchtitle='Book Three';
book = books(strcmpi( books(:,1), searchtitle), :);

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by