GUI Listbox - choose item in listbox and output corresponding variable in static
조회 수: 4 (최근 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:![](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
답변 (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!