How to create a dropdown menu in MATLAB
Last Updated :
22 Sep, 2021
In this article, we will learn about the dropDown menu component, how to create it and what are its important properties. Then we will create a simple Matlab Gui application.
Creating a dropdown menu in MATLAB
A Dropdown menu is a UI component that helps users to select one of the given options or type as text. Matlab provides a function named uidropdown to create an instance of the dropdown menu.
The three ways one can create a dropdown menu is as follows:
- dropdownObject = uidropdown; (without any parameters)
- dropdownObject = uidropdown(parent);
- dropdownObject = uidropdown(parent, NameOfProperty, Value ...);
Properties
Some important properties of this UI component are as follows:
- Items: The options available in the drop-down menu are set using this property.
- Value: The value that is currently selected by the component is accessed or changed.
- Text: The text property is used to control the content of the label. The default value is 'Label'.
- Interpreter: This property allows us to interpret the text of the label using a different interpreter. Like we can use latex code for writing equations or HTML for formatting as text and set the interpreter to be 'latex' or 'HTML'. Default is 'none.
- HorizontalAlignment: Controls the horizontal alignment of the text within the label component. Default is 'left'.
- VerticalAlignment: Controls the vertical alignment within the component. Default is 'center'.
- Wordwrap: Wraps the text to fit the component's width. Default is 'off.
- FontName: changes the font for the text.
- FontSize: controls the font size.
- Font-weight: controls the weight/boldness of the text.
- FontAngle: controls the font angle.
- Font color: the color of the font.
- BackgroundColor: change the background color of the label.
- Visible: This property controls the visibility of the component. Default is 'on'.
- Enable: Enable or Disabled appearance. Default is 'on'.
- TootTip: A text to guide the purpose of the component. Default is ''.
- Position: A 4 valued list that controls the location of the component within the parent window and the size of the component.
Let's see some examples demonstrating the use of syntaxes.
uidropdown()
This function does not require any parameter but as every component must be inside a parent window, Matlab creates a figure and assigns the component to it.
Example:
Matlab
% MATLAB code for uidropdown()
% create a uidropdown object using the api
dropdownObject = uidropdown;
Output:
A drop-down menu
by default, the component holds the items as 'option 1', 'option 2', and so on.
uidropdown(parent)
The method uidropdown also accepts an optional parent window to hold the component.
Example:
Matlab
% MATLAB code for uidropdown(parent)
% create a figure window
fig = uifigure;
% create a drop down menu and pass the fig as parent
dropdownObject = uidropdown(fig);
Output:
A drop-down component using a custom window as parentuidropdown(parent, NameOfProperty, Value ...)
 Matlab also provides us with the option to set the properties of the drop-down menu while instantiation by passing the property name and its value.
Example:
Matlab
% MATLAB code for uidropdown(parent, NameOfProperty, Value ...)
% create a figure window
fig = uifigure;
% create a drop down menu and pass the fig as
% parent as well as set the properties.
dropdownObject = uidropdown(fig, 'Items', {'Mango','Guava','Orange','Apple'},
'Value', 'Apple');
Output:
A drop-down menu with custom options
In the method uidropdown, the 'Items' property is set which is all the options that the component will display. The 'value' property describes the option that will be selected by default.
Now we create an application in which selecting an option from the drop-down menu will be displayed in a Label with bigger fonts.
Example:
Matlab
% MATLAB code for create a figure
fig = uifigure('Position', [100 100 300 275]);
% create a label
label = uilabel(fig, 'Position', [100 120, 120 40],...
'FontSize', 30,...
'FontWeight', 'bold');
% create a dropdownObject and pass the figure as parent
dropdownObject = uidropdown(fig,...
'Items', {'Mango','Guava','Orange','Apple'},...
'Value', 'Apple',...
'Editable', 'on',...
'Position', [84 204 100 20],...
'ValueChangedFcn', @(dd, event) fruitSelected(dd, label));
% function to call when option is selected (callback)
function fruitSelected(dd, label)
% read the value from the dropdown
val = dd.Value;
% set the text property of label
label.Text = val;
end
Output:
A drop-down menu with font propertyWe first create a figure to hold all the UI components and set its Position property that controls its position as well as its size. Then a label is created with a font size of 30 and font-weight set to bold. We create a dropdown menu and sets its properties like editable which enables users to enter a value instead of choosing an option. The 'ValueChangedFcn' property sets the function callback that will be called once the value of the drop-down menu changes.
Â
Similar Reads
How to create a dropdown menu in Bootstrap ?
A dropdown menu offers a list of alternatives when clicked or hovered on, which is a clean method of providing a list of alternatives as only one option is displayed initially onto the screen. Drop-down menus are used in almost all types of software nowadays to show sub-options of the option. Step 1
2 min read
How to create a function in MATLAB ?
A function is a block of statements that intend to perform a specific task. Functions allow the users to reuse the code frequently. MATLAB has several predefined functions which are ready to use such as sin(), fact(), cos() etc. MATLAB also allows the users to define their own functions. Syntax: fun
2 min read
How to Create Dropdown List in Google Sheets
Adding a dropdown list in Google Sheets can make your spreadsheets more dynamic and user-friendly. This feature allows you to create a list of options that users can select from, making data entry quicker and reducing errors. Whether you're managing a budget, tracking projects, or organizing data, d
9 min read
JavaScript - How to Create Dropdown List?
The dropdown list is a toggleable menu that allows to user to choose one option from multiple. The dropdown list is basically a button, when the user clicks on that, it expands downward and shows its sub-elements, from which users can select any one of them according to their preferences. Creating D
3 min read
How To Create A Dropup Menu Using CSS?
A Dropup menu is a type of menu in web design that expands upwards from its trigger element instead of the traditional dropdown direction. Dropup menus are often used in contexts such as navigation bars, form selections, and interactive elements. Below are the approaches used for creating the dropup
6 min read
How to create a textarea component in MATLAB
Matlab offers tools for developing GUI applications. It provides functions that can create TextFields, Labels, Buttons, and many more, along with properties to manipulate the components. In this article, we will learn to create a TextArea Component using Matlab. Creating a textarea componentA Text A
4 min read
How to style a dropdown using CSS?
We will learn how to style the dropdown list using CSS and explore its implementation through examples. Dropdown menus are commonly used to allow users to select an option from a predefined list. Styling them properly can enhance your website's user experience and visual appeal. What is a <select
3 min read
How to Add Drop Down List in Table Cell ?
Drop-downs are the user interface elements. Drop Down List is used to select one out of various options. This article focuses on creating a drop down list in the table cell which helps in selecting the choices directly from the table itself. Below are the approaches to add a Drop Down List in a Tabl
3 min read
How to Create an Image Component in MATLAB?
MATLAB is an extensive tool that provides various options to its users. Creating image components is one of those tools. MATLAB provides simple functions to create image components. The uiimage function creates a new image component in a new figure by calling on the uifigure function. Usage of uiima
2 min read
How to Create a Dependent Drop Down List in Excel
Dependent Down Lists allow you to validate the data and make the data entry and data filtering very smooth and time-saving. Dependent Down Lists increase the chances of fewer errors in one's worksheet. The prerequisites for learning Dependent Drop Down List are Create from Selection and Drop Down Li
9 min read