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

KSSV
KSSV 2017년 4월 26일
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

KSSV
KSSV 2017년 4월 26일
편집: KSSV 2017년 4월 26일

0 개 추천

prompt=input('Please enter title of the book: ', 's');
idx = find(strcmp({S.Title}, prompt)==1) ;
disp(S(idx))

댓글 수: 2

jsmith2017
jsmith2017 2017년 4월 26일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

질문:

2017년 4월 26일

편집:

2017년 4월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by