Trying to create a drop down that will open a new uifigure, nothing happens when I click confirm?

3 views (last 30 days)
Im writting a program to process data from different autonomous vehicles. I have 5 different vehicles that I can run and each of their data sets are recorded differently with different options, I have written a robust program that does alot of different things but only for 1 vehicle. Now im trying to expand this program but allowing the user to select the vehicle they ran and want to process. Below is what I have so far:
function [name, dd2_option] = programName()
%//////////////////////////////////////////////////////////////////////////
%////////////////////// VEHICLE SELECTION ////////////////////
%//////////////////////////////////////////////////////////////////////////
%create a small figure to show the pop up menu
fig1 = uifigure('Name', 'Drop Down Menu', 'Position', [500, 500, 200, 150]);
%figure text
uilabel(fig1, ...
'Text', 'Select Vehicle:', ...
'Position', [25, 90, 120, 30], ...
'FontSize', 14);
%drop down menu
dda = uidropdown(fig1, 'Position', [25, 50, 120, 30]);
dda.Items = ["Vehicle Selection", 'USV-1', 'USV-2', 'AUGV', 'UUV', 'ROV'];
dda.ItemsData = [0, 1, 2, 3, 4, 5];
%confirm button
btna = uibutton(fig1,'Position',[25, 10, 120, 30],'Text','Confirm','ButtonPushedFcn',@(btn, event) confirmSelection(dda.Value));
%function to determine which dd option was selected
function confirmSelection(selectedOption)
switch selectedOption
case 'USV-1'
usv1Figure()
case 'USV-2'
usv2Figure()
case 'AUGV'
augvFigure()
case 'UUV'
uuvFigure()
case 'ROV'
rovFigure()
end
end
function usv1Figure()
delete(fig1)
%start the program for USV1
When I run the code fig1 pops up with my drop down and I can select which option I want. Right now the only value that works is 'USV-1' option, when select that nothing happens. What I think it should do is close fig1 with the drop down and open fig2 to start my program. like I said, nothing happens, I dont get any warnings or errors in the command window, fig1 doesnt close, and fig2 doesnt open. Nothing happens. Where am I going wrong? Before I added this I had no issues running my program. Thanks in advance to any help you can give!

Accepted Answer

Cris LaPierre
Cris LaPierre on 7 Jan 2025
The value of selectedOption is a number corresponding to the item selected (e.g. 1, 2, 3, etc).
You need to update your cases accordingly, or use dda.Items{dda.Value+1}.
function [name, dd2_option] = programName()
%//////////////////////////////////////////////////////////////////////////
%////////////////////// VEHICLE SELECTION ////////////////////
%//////////////////////////////////////////////////////////////////////////
%create a small figure to show the pop up menu
fig1 = uifigure('Name', 'Drop Down Menu', 'Position', [500, 500, 200, 150],'HandleVisibility', 'on');
%figure text
uilabel(fig1, ...
'Text', 'Select Vehicle:', ...
'Position', [25, 90, 120, 30], ...
'FontSize', 14);
%drop down menu
dda = uidropdown(fig1, 'Position', [25, 50, 120, 30]);
dda.Items = ["Vehicle Selection", 'USV-1', 'USV-2', 'AUGV', 'UUV', 'ROV'];
dda.ItemsData = [0, 1, 2, 3, 4, 5];
%confirm button
btna = uibutton(fig1,'Position',[25, 10, 120, 30],'Text','Confirm','ButtonPushedFcn',@(btn, event) confirmSelection(dda.Items{dda.Value+1}));
%function to determine which dd option was selected
function confirmSelection(selectedOption)
switch selectedOption
case 'USV-1'
usv1Figure()
case 'USV-2'
usv2Figure()
case 'AUGV'
augvFigure()
case 'UUV'
uuvFigure()
case 'ROV'
rovFigure()
end
end
function usv1Figure()
delete(fig1)
%start the program for USV1
end
end
  2 Comments
Stephen23
Stephen23 on 7 Jan 2025
Edited: Stephen23 on 7 Jan 2025
"The value of selectedOption is a number corresponding to the item selected (e.g. 1, 2, 3, etc)."
Actually the .Value property is either
  • an element of Items (default), or
  • an element of ItemsData (if defined)
So in this case it would be one of 0, 1, 2, 3, 4, 5. But these values are quite arbitrary!
A more robust answer would simply use the .ValueIndex property (which again makes .ItemsData superfluous).

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 7 Jan 2025
Edited: Stephen23 on 7 Jan 2025
Simpler solution: get rid of dda.ItemsData. Then .Value is exactly the text that is shown in the menu.
In a nutshell: when you specify .ItemsData then .Value will be one of those "items". When you do not specify ItemData then it defautls to using the values of .Items, which is what you used as case values in the switch statement. Ergo, simply use the default by removing the completely superfluous .ItemsData.
function untitled01()
%//////////////////////////////////////////////////////////////////////////
%////////////////////// VEHICLE SELECTION ////////////////////
%//////////////////////////////////////////////////////////////////////////
%create a small figure to show the pop up menu
fig1 = uifigure('Name', 'Drop Down Menu', 'Position', [500, 500, 200, 150]);
%figure text
uilabel(fig1, ...
'Text', 'Select Vehicle:', ...
'Position', [25, 90, 120, 30], ...
'FontSize', 14);
%drop down menu
dda = uidropdown(fig1, 'Position', [25, 50, 120, 30]);
dda.Items = ["Vehicle Selection", "USV-1", "USV-2", "AUGV", "UUV", "ROV"];
%confirm button
uibutton(fig1,'Position',[25, 10, 120, 30],'Text','Confirm','ButtonPushedFcn',{@confirmSelection,dda});
%function to determine which dd option was selected
function confirmSelection(~,~,dda)
switch dda.Value
case 'USV-1'
usv1Figure()
case 'USV-2'
usv2Figure()
case 'AUGV'
augvFigure()
case 'UUV'
uuvFigure()
case 'ROV'
rovFigure()
end
end
end

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!