GUI Listbox - choose item in listbox and output corresponding variable in static

8 views (last 30 days)
As per the question,
I want to create a GUI that output a value in a static text when an item is choosen from the listbox.
Coding as follows:
function dataselection
fig = uifigure('Position',[0.6 0.8 0.2 0.5]);
% value to correspnding postition
% Create Numeric Edit Field
ef = uieditfield(fig,'numeric','Position',[0.6 0.8 0.2 0.5]);
% Create List Box
lbox = uilistbox(fig,'Items', {'Accident', 'Visit', 'Pharma', 'Optical'},'Position',[0.6 0.8 0.2 0.5],'ValueChangedFcn', @selectionChanged);
% ValueChangedFcn callback
function selectionChanged(src,event)
% Display list box data in edit field
ef.Value = src.Value;
end
end

Answers (1)

Cris LaPierre
Cris LaPierre on 18 Nov 2018
Edited: Cris LaPierre on 18 Nov 2018
For uilistbox, Value is a character array.
Value: 'Pharma'
Items: {'Accident' 'Visit' 'Pharma' 'Optical'}
ItemsData: []
Multiselect: 'off'
ValueChangedFcn: @dataselection/selectionChanged
Position: [150 150 100 100]
This means you have to either determine which number item it is before assigning a value to a numeric edit field, or your edit field should display text.
There are some other issues I fixed so be sure to take note of the differences between my code below and yours. I used the example on this page this page as a model.
function dataselection
fig = uifigure('Position',[300 100 400 400]);
% Create Numeric Edit Field
ef = uieditfield(fig,'Position',[150 50 100 20]);
% Create List Box
lbox = uilistbox(fig,'Items', {'Accident', 'Visit', 'Pharma', 'Optical'},...
'Position',[150 150 100 100],...
'ValueChangedFcn', @(lbox,event) selectionChanged(lbox,ef));
end
% ValueChangedFcn callback
function selectionChanged(lbox,ef)
% Display list box data in edit field
ef.Value = lbox.Value;
end

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!