Listbox and Pushbutton in GUI
5 次查看(过去 30 天)
显示 更早的评论
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;
i=handles.i;
axes(handles.plotty);
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))
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!!
0 个评论
采纳的回答
Adam
2016-7-29
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 个评论
Adam
2016-8-1
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.
listboxCallback(...)
listbox_index = get( hObject, 'Value' )
plotIdx = handles.i;
runCommonCode( handles.plotty, listbox_index, plotIdx )
pushbuttonCallback(...)
listbox_index = get( handles.listbox, 'Value' );
plotIdx = someLogicForNext(...);
runCommonCode( handles.plotty, listbox_index, plotIdx )
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!