Choice made in menu prints a blank box

2 views (last 30 days)
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.

Answers (1)

Pranjal Priyadarshi
Pranjal Priyadarshi on 8 Feb 2019
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

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!