How to Create a Dropdown Menu in MATLAB


In this article, we will learn how to create a dropdown menu/list in MATLAB. In MATLAB, a dropdown menu is a user interface (UI) component allows users to choose one the multiple options or to type as text.

In MATLAB, we have a built−in function named ‘unidropdown’ that allows to create a dropdown menu. This function can have different syntaxes to create different types of dropdown menu.

The most commonly used types of dropdown menu are as follows:

  • Dropdown menu without any parameters

  • Dropdown menu with a specific parent container

  • Dropdown menu with a specific parent container and properties

Let us now discuss the creation of each type of dropdown menu in MATLAB.

Dropdown Menu without Any Parameter

In MATLAB, we can create a dropdown menu with default properties. This type of dropdown menu does not contain any options initially.

The following syntax of the ‘uidropdown’ function is used to create a dropdown menu with default properties:

dropdown_menu = uidropdown;

This syntax will create a dropdown menu with no options.

The following MATLAB program demonstrates the code implementation for this syntax.

Example

% MATLAB program to create a dropdown list with default properties
% Create a dropdown menu with no options
dropdown_menu = uidropdown;

Output

Dropdown Menu with a Specified Parent Container

In MATLAB, we can also create a dropdown menu with a specified parent container. Where, the parent container can be a figure or panel or any other kind of container which holds the dropdown menu. This type of dropdown menu is generally created when we want to insert the dropdown menu at a specific location in our GUI window.

To create a dropdown menu with a specified parent container, we use the following syntax of the ‘uidropdown’ function:

dropdown_menu = uidropdown(parent);

The following MATLAB program demonstrates a code implementation to create a dropdown menu with a figure as the parent container.

Example

% MATLAB code to create a dropdown menu with a figure as parent container
% Create a figure to hold the dropdown menu
f = uifigure;

% Create a dropdown menu with figure as parent container
dropdown_menu = uidropdown(f);

Output

Dropdown Menu with Specified Parent Container and Properties

In MATLAB, we can also create a dropdown menu with the specified parent container and properties of the menu. For this, we use the following syntax of the ‘uidropdown’ function:

dropdown_menu = uidropdown(parent, 'property_name', value);

Using this syntax of the ‘uidropdown’ function, we can apply various properties such as list options, position, etc. of the dropdown menu.

The following MATLAB program demonstrates the implementation of this syntax to create a dropdown with specified parent container and properties.

Example

% MATLAB code to create a dropdown menu with specified parent container and properties
% Create a figure as the parent container
f = uifigure;

% Create a option list for dropdown menu
colors = {'Red', 'Green', 'Blue', 'Pink'};

% Create dropdown menu with specified parent container and properties
dropdown_menu = uidropdown(f, 'Items', colors,  'Position', [50, 50, 150, 50]);

Output

Code Explanation

In this MATLAB code, we start by creating a figure as a parent container to hold the dropdown menu using the ‘uifigure’ option.

Next, we create a list of options for the dropdown menu and store it in a variable ‘colors’.

Finally, we create a dropdown menu with the specified parent container and some other properties like items and position. Here, the ‘Item’ property sets the options of the dropdown menu to the ‘colors’. The ‘Position’ property sets the position and size of the dropdown menu. In the value array of the position property ‘[50, 50, 150, 50]’ specifies the X−coordinate, Y−coordinate, width, and height of the dropdown menu respectively.

Conclusion

Hence, this is all about creating a dropdown menu in MATLAB. We can use the MATLAB’s built−in function ‘uidropdown’ to create a dropdown menu. It is a user interface component which allows users to select an option from a list of options.

Updated on: 08-Aug-2023

144 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements