MATLAB Answers


[GUI] How do you get only one element in a listbox

Asked by Shane
on 17 Dec 2012
I know you are able to obtain all the elements in a string from the listbox with:
get(handles.listbox1, 'String')
But what if I only want the second or fourth element in the listbox?
Thanks in advance <3


Sign in to comment.


1 Answer

Answer by Image Analyst
on 17 Dec 2012
 Accepted Answer

You have to read in the whole thing anyway. Then get the value property and use that to index into the string.
listboxItems = get(handles.listbox1, 'string');
selectedItems = get(handles.listbox1, 'value');
selectedItem = listboxItems(selectedItems);


Show 1 older comment
Ok I understand it now, Although I only get the first letter of the element.
and not the whole element?
So if the elements are "fish, dog, goat, rhino" and I select dog and hit my push button I only get "d" as my output and not dog.
listboxItems = get(handles.listbox1, 'string');
listboxItems = cellstr( get(handles.listbox1, 'string') );
Thank you soo much Walter I appreciate it!!!

Sign in to comment.