MATLAB is a matrix-based computing environment with its own programming language that is simple to learn and use. With the help of MATLAB’s GUI Graphical User Interface, it is utilized to understand complex mathematical ideas and large data sets.
List boxes are user interface elements that display items in a list. A list box’s appearance and behavior are controlled by its properties. To refer to a specific object or property.
Example 1:
% MATLAB Code function dataselectionbox
fig = uifigure( 'Position' ,[200 200 650 475]);
% Making a Numeric Edit Field ef = uieditfield(fig, 'numeric' ,...
'Position' ,[250 180 200 44]);
% Making a List Box lbox = uilistbox(fig,... 'Items' , { 'GFG1' , 'GFG2' , 'GFG3' , 'GFG3' },...
'ItemsData' , [0, 25, 40, 100],...
'Position' ,[250 240 200 156],...
'ValueChangedFcn' );
% Callback to a function that has had its value changed function selectionChanged(src,event)
% In the edit field, show the data from the list box.
ef.Value = src.Value;
end end |
Output:
Select an item from the list with data selection. The temperature linked with the selection is updated in the numeric edit box.
Now we see ListBox GUI based app in MATLAB. For this we select Design App option with ListBox option in MATLAB.
Example 2:
% MATLAB code for ListBox GUI classdef appList < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
ListBoxGUIdemoLabel matlab.ui.control.Label
EditField3 matlab.ui.control.NumericEditField
ResultLabel matlab.ui.control.Label
EnterSecondNumberLabel matlab.ui.control.Label
EnterFirstNumberLabel matlab.ui.control.Label
EditField2 matlab.ui.control.EditField
EditField matlab.ui.control.EditField
ListListBox matlab.ui.control.ListBox
ListListBoxLabel matlab.ui.control.Label
end
% Callbacks that handle component events
methods (Access = private)
% Callback function
function CSESubjectListBoxValueChanged(app, event)
end
% Value changed function: ListListBox
function List1.ListBoxValueChanged(app, event)
value = app.List1.ListBox.Value;
value = get(handles.ListBox, 'value' );
A Label = get(handles.edit1. 'String' );
B label = get(handles.edit1. 'String' );
A Label = Str2num(A);
B Label = Str2num(B);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure( 'Visible' , 'off' );
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App' ;
% Create ListListBoxLabel
app.ListListBoxLabel = uilabel(app.UIFigure);
app.ListListBoxLabel.HorizontalAlignment = 'right' ;
app.ListListBoxLabel.Position = [73 178 48 22];
app.ListListBoxLabel.Text = 'List ' ;
% Create ListListBox
app.ListListBox = uilistbox(app.UIFigure);
app.ListListBox.Items = { 'Add' , 'Sub' , 'Multiply' , 'Division' };
app.ListListBox.ValueChangedFcn = createCallbackFcn(app,
@ListListBoxValueChanged, true);
app.ListListBox.Position = [186 63 284 149];
app.ListListBox.Value = 'Add' ;
% Create EditField
app.EditField = uieditfield(app.UIFigure, 'text' );
app.EditField.Position = [307 356 163 45];
% Create EditField2
app.EditField2 = uieditfield(app.UIFigure, 'text' );
app.EditField2.Position = [307 303 163 44];
% Create EnterFirstNumberLabel
app.EnterFirstNumberLabel = uilabel(app.UIFigure);
app.EnterFirstNumberLabel.Position = [73 356 156 45];
app.EnterFirstNumberLabel.Text = 'Enter First Number' ;
% Create EnterSecondNumberLabel
app.EnterSecondNumberLabel = uilabel(app.UIFigure);
app.EnterSecondNumberLabel.Position = [71 303 132 44];
app.EnterSecondNumberLabel.Text = 'Enter Second Number' ;
% Create ResultLabel
app.ResultLabel = uilabel(app.UIFigure);
app.ResultLabel.Position = [73 242 178 36];
app.ResultLabel.Text = 'Result' ;
% Create EditField3
app.EditField3 = uieditfield(app.UIFigure, 'numeric' );
app.EditField3.Position = [307 231 163 43];
% Create ListBoxGUIdemoLabel
app.ListBoxGUIdemoLabel = uilabel(app.UIFigure);
app.ListBoxGUIdemoLabel.BackgroundColor = [0.3922 0.8314 0.0745];
app.ListBoxGUIdemoLabel.FontWeight = 'bold' ;
app.ListBoxGUIdemoLabel.Position = [71 426 420 33];
app.ListBoxGUIdemoLabel.Text = 'List Box GUI demo' ;
% Show the figure after all components are created
app.UIFigure.Visible = 'on' ;
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = appList
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end |
Output: