How to select different options in the same listbox and save to thier respective variables?

I want to select 3 different items from the same list box and save it to 3 different handles.
opt1 = handles.opt1;
opt2 = handles.opt2;
opt3 = handles.opt3;
opt = [opt1,opt2,opt3];
But when I select three different strings, I get only the 'opt3' as 'opt' output. I don't want to create 3 list boxes for the same options and make the GUI look messy. How do I do it? Please Help

 Risposta accettata

The example below shows how to isolate the selected items using the callback function to the listbox. If you produced the listbox using GUIDE, focus on the callback function in my example and skip the first half that produces the listbox.
% Create listbox
function myList
c = dialog();
listbox1 = uicontrol('Parent',c,...
'Style','listbox',...
'Position',[90 90 100 100],...
'Min', 0, ...
'Max',3,...
'String',{'Lancaster', 'Cincinnati', 'Sofia', 'Rochester'},...
'Callback', @listbox1_Callback);
% Callback function
function listbox1_Callback(hObject, eventdata)
mySelection = hObject.String(hObject.Value);
if length(mySelection)<3
return
end
opt1 = mySelection{1};
opt2 = mySelection{2};
opt3 = mySelection{3};
*Note that you'll need to adapt the callback function if the user is allowed to select less or more than 3 options.

14 Commenti

Hey Thank you so much for the answer. In fact I already created lisbox with the help of guide. So in listbox1_Callback(hObject, eventdat) function when I paste the following code:
mySelection = hObject.String(hObject.Value);
opt1 = mySelection{1};
opt2 = mySelection{2};
opt3 = mySelection{3};
I get 'Index exceeds array bounds' as an error. What can be done? Please let me know.
It's probably the case that the user selected < 3 options. As the note mentions at the end of my answer, you'll need to adapt the code to allow for less than 3 selections
Option 1 is to use simple conditionals.
if length(mySelection) == 2
opt2 = mySelection{2};
end
Repeat for if 3...
Option 2 (the better option) is to replace all of your optx variables with
mySelection{x};
Thank you for u r response. I appreciate it. But I am still stuck in getting the answer. After referring to your code I just replicated it as shown below
function listbox1_Callback(hObject, eventdata, handles)
mySelection = hObject.String(hObject.Value);
opt1 = mySelection{1};
opt2 = mySelection{2};
opt3 = mySelection{3}
And my available listbox1 options are:
Where I need to choose any of the 3 items listed in a group of 5. I get an error saying:
Index exceeds array bounds.
Error in GUI>listbox1_Callback (line 188) opt2 = mySelection{2};
There are two issues here.
First, in the image you provided you've only selected 1 item. In order to select more than 1 item you need to edit the listbox in GUIDE and set the 'max' parameter to 3 if you want to select up to 3.
Second, you haven't implemented either of the options I proposed. See my comments previous to this one. Note that if your selection will always be 3, you won't need to implement these options I proposed.
Thank you again. Yes indeed, I need to select 3 items always. I set the parameters in the listbox property as you said:
But I still get the same error. Any more help please?
Ok, I see what the problem is. Every time you make a selection the callback function is executed and since you can only click 1 option at a time, you're always getting just 1 selection.
So, if your selection will always be 3, an easy fix is to simply ignore the callback function whenever the selection is <3. Here's what your callback function should do:
function listbox1_Callback(hObject, eventdata)
mySelection = hObject.String(hObject.Value);
if length(mySelection)<3
return
end
opt1 = mySelection{1};
opt2 = mySelection{2};
opt3 = mySelection{3};
Now I can select any option but nothing is getting reflected in the code. I checked it by applying break point as well.
Change the 'min' parameter back to 0 and keep the max at 3. After you do that, you can select 1 or 2 items and nothing will happen until you select 3 items.
Still No result :(
Have you tried in your matlab with simple list function and applying this logic? If yes then can you please share the scree shots here. I will try to analyze it. Don't we need to use GET function? Thank you so much :)
I just updated the code in my original answer to include the min=0 and the conditional within the callback function. It definitely works. The next step is to triple-check that your listbox has the correct parameters ('Min', 0, ... 'Max',3,...).
Your callback function above looks OK. If you're still having the error after you made those changes and have selected 3 options, tell me the specific error, the line of code that's generating the error, and attach the code and GUI if possible (or at least share the lines that are causing the error).
Oh man It finally worked :) My goodness, I wasn't selecting the items using the 'Ctrl' button. Thanks for the your input. I saw the scree shot of yours and got the idea. Thanks again for being patient.
You can use the mouse to click and drag or use the ctrl button to select >1 option. Glad it all worked out.
Also, I had one more issue. I posted the question but no one replied.I always see the top option in the listbox is default selected. How to remove this default selection?
Please help me. I checked the property inspector but couldn't find any solution. Thanks!
I just replied to your question in your original thread.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Interactive Control and Callbacks in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by