Choice made in menu prints a blank box

1 次查看(过去 30 天)
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
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 CenterFile Exchange 中查找有关 String Parsing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by