MATLAB Answers

How to pre-select a value in a list box (GUI)

조회 수: 3(최근 30일)
Yahel Oren
Yahel Oren 2021년 8월 1일
답변: Image Analyst 2021년 8월 1일
I would like to pre-select a value (lets say - value number 45 in an array is the default value) and when I open the GUI I want that value to be selected (with the blue mark in the list box)
I tried to set the Value to my value but it's not working
Thank you!

채택된 답변

Image Analyst
Image Analyst 2021년 8월 1일
You can set the index of the listbox in your startup code, yourApp_OpeningFcn() if you're using GUIDE.
index = 45;
handles.listbox1.Value = 45;
If you prepopulated the listbox with strings and want to find out which one(s) has 45, you can do this:
listBoxItems = handles.listbox1.String; % e.g. {'4'; '45'; '5'; '45'}
pattern = '45';
indexes = ismember(listBoxItems, pattern) % e.g. 0 1 0 1
handles.listbox1.Value = find(indexes) % e.g. 2 4

추가 답변(1개)

dpb
dpb 2021년 8월 1일
You mean a listdlg box? The 'InitialValue' property (default value 1) is the index to the selected element(s) when the box is opened.
If 'SelectionMode' is 'single' then it is a scalar index value, if 'multiple' it can be a vector of indices.
Again, it is the index to the desired value(s), NOT the value itself, at it is not the value returned which is also an index, not a value.

Community Treasure Hunt

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

Start Hunting!

Translated by