Listbox and Pushbutton in GUI

3 views (last 30 days)
chlor thanks
chlor thanks on 27 Jul 2016
Commented: chlor thanks on 1 Aug 2016
I have a listbox(tag:listbox) that gives different choices of plotting(axes tag:plotty), and a pushbutton(tag: NextButton) called "next" which execute the choice that the listbox made. My code in GUI looks something like this:
%some code
handles.i = 1;
handles.k = length(files);
handles.files = files;
guidata(hObject, handles);
% --- Executes on selection change in plotbox.
function listbox_Callback(hObject, eventdata, handles)
files = handles.files;
listbox_index=get(hObject, 'Value');
switch listbox_index
case 1
[data,~,~] = xlsread(files{i});
x = 1:6;
plot(x, data(1:end))
case 2
[data,~,~] = xlsread(files{i});
x = 2:7;
plot(x, data(1:end))
case 3
[data,~,~] = xlsread(files{i});
x = 4:9;
plot(x, data(1:end))
% --- Executes on button press in NextButton.
function NextButton_Callback(hObject, eventdata, handles)
%some code
guidata(hObject, handles)
After some fig test run, the GUI is able to run but not correctly---it seems that my listbox function and pushbutton works separately.
No matter which choice I made in the listbox. The pushbutton automatically execute whats in listbox function case 1, and never goes to case 2 and 3.
I would like to fix the code, any clue is appreciated. Thank you!!

Accepted Answer

Adam on 29 Jul 2016
You should never call one uicontrol's callback from inside another one and certainly not passing down the hObject of the first one.
Your listbox callback is actually getting called on the pushbutton object (the 'hObject' that was passed in) whose value will never change.
Why do you have a listbox callback and a button that will also trigger the same functionality? Do you want the callback to trigger when the listbox changes even if you don't press the button?
If not you should have the main piece of code in your pushbutton callback and you should refer to the listbox by its tag as:
get( handles.listbox, 'Value' )
chlor thanks
chlor thanks on 1 Aug 2016
This works! Thank you sir :D

Sign in to comment.

More Answers (0)


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!

Translated by