I have 2 listboxes that I need to link.

2 views (last 30 days)
Meaning that when I select the first index from the first listbox it should display certain data on second listbox and same thing when I select second index from first listbox it should display some other data in the second listbox. How do I link them?
For Eg: The first listbox has entries of Fruits, Vegetables, Meat Then when i select Fruits i want to display, apples and oranges in second listbox or when i select Vegetables i want to display broccoli, lettuce and kale etc.... I was thinking that you can use cells maybe?
Thank you in advance...
  2 Comments
jgg
jgg on 6 Jan 2016
This seems a little abstract, but couldn't you do something like this:
[s,v] = listdlg('PromptString','Select a file:',...
'SelectionMode','single',...
'ListString',categories.name)
Where categories is the Fruits, Veggies, Meats, set. Then, the return value s can be used to switch between the other ListString in the other list box.
I did this on my PC by setting up a struct which is a set of categories:
categories = struct();
categories.name = {'Fruits', 'Veggies', 'Meat'}
veggies = struct();
veggies.name = {'Potato', 'Carrot'}
... etc etc
Then, in the first listbox, I did
[s,v] = listdlg('PromptString','Select a file:','SelectionMode','single','ListString',categories.name)
switch s
case 1
str = veggies.name
case 2
str = fruits.name
%etc etc etc
end
Then, in my next listbox I did:
[s,v] = listdlg('PromptString','Select a file:',...
'SelectionMode','single',...
'ListString',str)
This works, but it's not implemented within a GUI. You'll probably need an event handler to execute the switching code, which I can't really help with since I don't know your GUI (and this isn't my strongest suite).
Image Analyst
Image Analyst on 7 Jan 2016
categories is a built-in function name so you'll want to use another name for the structure variable.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 7 Jan 2016
In the callback for listbox1, have this:
selection = get(handles.listbox1, 'Value');
% 1 = Fruits, 2 = Vegetables, 3 = Meat
if selection == 1
% Fruits selected
listBoxContents = {'apples', 'oranges');
elseif selection == 2
% Vegetables selected
listBoxContents = {'broccoli', 'lettuce', 'kale'};
else
% Meat selected
listBoxContents = {'beef', 'poultry', 'pork', 'fish', 'platypus'};
end
% Send that list to listbox 2
set(handles.listbox2, 'String', listBoxContents);
guidata(hObject, handles);
  2 Comments
Jason Salim
Jason Salim on 7 Jan 2016
Thanks. I think this make sense. What if listBoxContents are going to vary? Meaning that user can add new list. For eg: i want to add peach in listbox 2 under fruits.
Basically I have 2 listboxes that can be customized (by adding entries for both listboxes by users by clicking on 2 buttons each for individual list boxes) and also they have to be linked.
Thanks again!
Image Analyst
Image Analyst on 7 Jan 2016
You can do that. You just have to keep track of the lists somehow. Like have them be global variables and if they click a button, call inputdlg() and ask for the new name to add to the list. Read the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!