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

10 次查看(过去 30 天)
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.

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by