필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
ET1994
ET1994 2018년 11월 17일
편집: Cris LaPierre 2018년 11월 18일
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

답변 (1개)

Cris LaPierre
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

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by