GUI Listbox - choose item in listbox and output corresponding variable in static
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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:![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/195494/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/195494/image.jpeg)
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 commentaires
Réponses (1)
Cris LaPierre
le 18 Nov 2018
Modifié(e) : Cris LaPierre
le 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
0 commentaires
Voir également
Catégories
En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!