Listbox and Pushbutton in GUI

8 vues (au cours des 30 derniers jours)
chlor thanks
chlor thanks le 27 Juil 2016
Commenté : chlor thanks le 1 Août 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!!

Réponse acceptée

Adam le 29 Juil 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' )
  5 commentaires
Adam le 1 Août 2016
Well, the general idea is that if you want to do the same thing under a listbox callback and a pushbutton callback then you want that code somewhere that you can call from either. Then in the callbacks themselves you have to deal with getting the information you need.
In the listbox callback this may be just the listbox value, in the pushbutton callback it may be still the listbox value, but also the whatever information it is that your button wants to do differently from just using the listbox callback.
In your case I assume that the information you get from the pushbutton is the plot number, to go with the case number retrieved from the listbox.
When you trigger the functionality from the listbox you presumably still need to get the plot number from somewhere though - e.g.
listbox_index = get( hObject, 'Value' )
plotIdx = handles.i;
runCommonCode( handles.plotty, listbox_index, plotIdx )
listbox_index = get( handles.listbox, 'Value' );
plotIdx = someLogicForNext(...);
runCommonCode( handles.plotty, listbox_index, plotIdx )
chlor thanks
chlor thanks le 1 Août 2016
This works! Thank you sir :D

Connectez-vous pour commenter.

Plus de réponses (0)


En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by