Effacer les filtres
Effacer les filtres

How to make a dropdown menu

17 vues (au cours des 30 derniers jours)
Aya Zaatreh
Aya Zaatreh le 9 Avr 2024
Modifié(e) : Voss le 9 Avr 2024
I am trying to make a dropdown menu that a user choses from and based on their selection the final calculation is adjusted accordingly. Below is the pasted code, originally I was having problems where only the invalid selection was being made, but now the dropdown menu doesn't even come up. Any information would be helpful!
function dropdown()
figure('Position', [200, 200, 300, 200]);
dropdownOptions = {'0 minutes', '10 minutes', '20 minutes', '30 minutes', '40+ minutes'};
dropdown = uicontrol('Style', 'popupmenu','String', dropdownOptions, 'Position', [50, 100, 200, 30], 'Callback', @dropdownCallback);
uicontrol('Style', 'text','Position', [50, 50, 200, 30]);
volume = 0;
function dropdownCallback(~, ~)
selectedOption = get(dropdown, 'Value');
switch selectedOption
case '0 minutes'
volume = TotalVolume;
case '10 minutes'
volume = 1.25 * TotalVolume;
case '20 minutes'
volume = 2.93 * TotalVolume;
case '30 minutes'
volume = 3.14 * TotalVolume;
case '40+ minutes'
volume = 4 * TotalVolume;
otherwise
error('Invalid selection');
end
end
disp(volume);
end

Réponses (1)

Voss
Voss le 9 Avr 2024
"I was having problems where only the invalid selection was being made"
That's because, in the words of the uicontrol properties documentation, "Value property equals an array index corresponding to the selected item in the pop-up menu. A value of 1 corresponds to the first item in the pop-up menu."
This means that your switch/case should look something like this:
selectedOption = get(dropdown, 'Value');
switch selectedOption
case 1 % '0 minutes'
volume = TotalVolume;
case 2 % '10 minutes'
volume = 1.25 * TotalVolume;
case 3 % '20 minutes'
volume = 2.93 * TotalVolume;
case 4 % '30 minutes'
volume = 3.14 * TotalVolume;
case 5 % '40+ minutes'
volume = 4 * TotalVolume;
otherwise
error('Invalid selection');
end
(The way you had it would've been appropriate for a uidropdown object, which is a different type of object from a popupmenu-style uicontrol. See, for reference: https://www.mathworks.com/help/matlab/ref/matlab.ui.control.dropdown-properties.html)
Of course, now the problem is that the variable TotalVolume is undefined. I don't know where that's supposed to come from.
"now the dropdown menu doesn't even come up"
That happens if the popupmenu Value is invalid, e.g., because it's outside the range of the String or not an integer. In this situation, unless you've turned off warning(s), you'll see the following in the command window:
Warning: 'popupmenu' control requires that 'Value' be an integer within
Character vector range
Control will not be rendered until all of its parameter values are valid
I don't see anything in the code you've shared that would cause that though.

Catégories

En savoir plus sur 3-D Volumetric Image Processing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by