Get multiples item from listbox

조회 수: 11 (최근 30일)
Nicolas Giret
Nicolas Giret 2017년 2월 2일
댓글: Nicolas Giret 2017년 2월 2일
Dear all,
I have a listbox in a GUI from which I want to retrieve the value of different items. I have set up the "Max" value to 2 (and the "Min" to 0). With this I can select 2 items in my list. But, when I want to get the value of the two selected items with:
contents = cellstr(get(hObject,'String'))
selected_file=contents{get(hObject,'Value')}
It is only giving me the value of the first item in the list as a string.
How I can get an array of the two selected items then? I'm sure it is very simple but I can't find the solution.
Thanks for your help.

답변 (3개)

Adam
Adam 2017년 2월 2일
편집: Adam 2017년 2월 2일
selected_file=contents( get(hObject,'Value') )
should work. Or just
selected_file = hObject.String( hObject.Value );
in one line (R2014b or later - the equivalent get style works for earlier versions)

Nicolas Giret
Nicolas Giret 2017년 2월 2일
Thanks Adam,
It is indeed giving the "Value" ([1 2] if I select the two first items).
But actually, I wasn't clear enough. Instead of the "Value", I rather wanted to get the "String" of these 2 items which I get with:
contents = (get(hObject,'Value'))
str_contents = cellstr(get(hObject,'String'))
selected_items=str_contents(contents)
Perhaps it is not really elegant, but it's working !
  댓글 수: 1
Adam
Adam 2017년 2월 2일
Well in your original question you extracted the string into contents so I used what you had written there and didn't bother to repeat it.

댓글을 달려면 로그인하십시오.


Jan
Jan 2017년 2월 2일
편집: Jan 2017년 2월 2일
Older Matlab versions contained the function popupstr for this job. Unfortunately this small but useful piece of code has been removed. Here is a replacement, which handles single and multi-selction listboxes and popup menus, strings, CHAR matrices and cell strings:
selected_items = GetSelection(hObject)
  댓글 수: 1
Nicolas Giret
Nicolas Giret 2017년 2월 2일
Thanks Jan, good to know.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by