How to get Handle of an list's element selected

4 views (last 30 days)
Hello, I want to know how can I get the handle of the element when I select it in the list for exemple, thank you

Answers (1)

Paulo Silva
Paulo Silva on 20 Apr 2011
In case you are referring to the List Box this is what I found in the documentation:
When the list box Callback callback is triggered, the list box Value property contains the index of the selected item, where 1 corresponds to the first item in the list. The String property contains the list as a cell array of strings.
This example retrieves the selected string. It assumes listbox1 is the value of the Tag property. Note that it is necessary to convert the value returned from the String property from a cell array to a string.
function listbox1_Callback(hObject, eventdata, handles)
index_selected = get(hObject,'Value');
list = get(hObject,'String');
item_selected = list{index_selected}; % Convert from cell array
% to string
You can also select a list item programmatically by setting the list box Value property to the index of the desired item. For example,
set(handles.listbox1,'Value',2)
selects the second item in the list box with Tag property listbox1.
  3 Comments
Paulo Silva
Paulo Silva on 20 Apr 2011
I might be wrong but the selected elements don't have their own handle, you must use the handle for the list box and the Value attribute.
Walter Roberson
Walter Roberson on 20 Apr 2011
Paulo is correct: selected entries do not have their own handles. They do not have their own properties either.
Sometimes it would be nice if the entries of a listbox or popup could be text boxes so you could use latex for fancier labels, but that isn't implemented.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!