Choice made in menu prints a blank box

1 回表示 (過去 30 日間)
Shawn Christensen
Shawn Christensen 2019 年 1 月 30 日
回答済み: Pranjal Priyadarshi 2019 年 2 月 8 日
Part c
% uses the menu command to ask the user wheather
% they hear "Yanny" or "Laurel", then stores their
% answer to the variable D3
first = 'Yanny';
second = 'Laurel';
D3 = menu('Who do you hear?',first,second);
% Part d
% Prints the results of the choice made in the menu
% which was stored in D3
disp(sprintf('D3 = \n%s',D3))
This prints a blank box and stores D3 as 1 or 2 depending on the selection made.
I need it to print Yanny or Laurel not 1,2 or a blank box.
Homework Question:
Write a line of code that shows the variable D3 and its value, without using fprintf.

回答 (1 件)

Pranjal Priyadarshi
Pranjal Priyadarshi 2019 年 2 月 8 日
In order to get the values of the options instead of the index we can pass the values in a cell array to the menu function. The output can be achieved in the following manner (without using the fprintf function):
This code should do the job for you.
s={'Yanny','Laural'};
D3=menu('Who do you hear?', s);
if(D3 == 0)
disp('No choice selected');
else
disp(sprintf('D3= \n%s',s{D3}));
end

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by