uidropdown(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:
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:
We 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.
How to create a dropdown menu in MATLAB
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.
Contact Us