MATLAB Answers

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

2 views (last 30 days)
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!

Accepted Answer

Image Analyst
Image Analyst on 1 Aug 2021
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

More Answers (1)

dpb on 1 Aug 2021
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