Indexing in a cell array and deleting it.

Hello. I am currently creating a library in Matlab and I'm using a structural array. For my library, I am trying to find a book by title and then display the book with title, author and number of pages. However currently it doesn't display the book, when i do it. The program adds a book to the library and then I can find that same book and display it with the title, author and page. However the current format I have does not display the book. Is it possible to isolate the book when its prompt and display it and/or delete it? Thanks for anyone who answers.
function library20
k = 0;
S = struct(); % creates a structural array
str = 'X';
while ~isempty(str)
str = input('What would you like to do: ', 's'); % asks user for a prompt
switch lower(str)
case 'add books'
k = k+1;
S(k).Title = input('Title: ', 's');
S(k).Author = input('Author: ', 's');
S(k).Pages = input('Number of Pages: ', 's');
fprintf('%s, %s, %s has ben added to the library\n', S(k).Title, S(k).Author, S(k).Pages)
case 'find book by title'
prompt=input('Please enter title of the book: ', 's');
if any(strcmp(S, 'prompt'))
disp(S)
end
end
end

1 comentario

KSSV
KSSV el 26 de Abr. de 2017
Accept the answers for your questions and then ask the other questions. On the same code you have asked three questions and you have not accepted any answer, though you have used the code which users have given.

Iniciar sesión para comentar.

 Respuesta aceptada

KSSV
KSSV el 26 de Abr. de 2017
Editada: KSSV el 26 de Abr. de 2017
prompt=input('Please enter title of the book: ', 's');
idx = find(strcmp({S.Title}, prompt)==1) ;
disp(S(idx))

2 comentarios

jsmith2017
jsmith2017 el 26 de Abr. de 2017
KSSV. The solution provided doesn't change anything and produces the exact same result. Nothing is printed out and it provides a logical array.
k = 0;
S = struct(); % creates a structural array
str = 'X';
while ~isempty(str)
str = input('What would you like to do: ', 's'); % asks user for a prompt
switch lower(str)
case 'add books'
k = k+1;
S(k).Title = input('Title: ', 's');
S(k).Author = input('Author: ', 's');
S(k).Pages = input('Number of Pages: ', 's');
fprintf('%s, %s, %s has ben added to the library\n', S(k).Title, S(k).Author, S(k).Pages)
case 'find book by title'
prompt=input('Please enter title of the book: ', 's');
idx = find(strcmp({S.Title}, prompt)==1) ;
disp(S(idx))
end
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 26 de Abr. de 2017

Editada:

el 26 de Abr. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by