GUI Listbox - choose item in listbox and output corresponding variable in static
6 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Cris LaPierre
2018-11-18
编辑:Cris LaPierre
2018-11-18
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!