Calling a function from within a GUI (GUIDE)
显示 更早的评论
%Psuedo Code example of what I'm trying to do
%Guide auto generated code here
%Radio Button Group
function FuncSetA_SelectionChangeFcn(hObject, eventdata, handles)
%Get numbers from user input into edit boxes
Edit_Box1 = get(str2double(get(handles.Edit_Box1,'string'));
Edit_Box2 = get(str2double(get(handles.Edit_Box2,'string'));
%Add or subtract the numbers from the Edit_boxes
switch get(eventdata.NewValue,'Tag') % Get Tag of selected object.
case 'add'
%For example: A = Edit_Box1 + Edit_Box2
case 'subtract'
%For example: B = Edit_Box1 - Edit_Box2
end
%Edit Box 1 uicontrol
function Edit_Box1_Callback(hObject, eventdata, handles)
%Edit Box 2 uicontrol
function Edit_Box2_Callback(hObject, eventdata, handles)
Pushbutton to display(A or B)
EOF
Question: This pseudo code works the first time I run it. However, if I change the edit box values and hit pushbutton to display A, it does not run the SelectChangeFcn again and thus does not update the values. I must change the edit box values, then toggle back and forth with the radio buttons and then hit the pushbutton to get it to display the correct answer.
Is there a way to force the code to run the SelectChangeFcn every time I hit the pushbutton (or equivalent)? That is I need it to update Edit_box1 and 2 values add them and then display upon the pushbutton call.
采纳的回答
更多回答(1 个)
John Petersen
2012-11-1
1 个投票
The functions in your program created by guide can be called like any other function. You just have to input the arguments it needs. I suggest you call the selectchange fcn from the pushbutton function, but you'll need to include the arguments it asks for.
8 个评论
Brad
2012-11-1
Brad
2012-11-2
Image Analyst
2012-11-2
You never need to mess with hObject or eventdata. Just access whatever control you want, even radio buttons, via their actual handle. So don't get the 'tag' of eventdata.newvalue, just check the radio button directly:
if get(handles.radioButton1, 'value')
% Do whatever you want to do if radio button 1 is selected.
elseif get(handles.radioButton2, 'value')
% Do whatever you want to do if radio button 2 is selected.
end
Brad
2012-11-4
John Petersen
2012-11-6
编辑:John Petersen
2012-11-6
@Image Analyst: on never needing to mess...: What if you want to perform the functions of a certain button(or several) automatically with another button? Seems like the simplest thing is to call the callback function for the button of interest. His original question was to know if there was a way to force calling the select function using a different button.
Image Analyst
2012-11-6
The way I handle that (no pun intended) is to have a separate custom function that I wrote myself, which takes handles as an input, and, if it made any changes to handles, return it as an output so guidata() can later update the handles structure globally. So you don't have one call back call another callback, say button1_click() call button2_click which does stuff, I have button1_click() and button2_click both call a separate function called A_button_was_clicked(handles). Then this function gets called by both functions and does what you want it to do - what button2_click would have done. This avoids the problem and confusion that might happen if button1_click called button2_click directly and so the hObject and eventdata that button 2 thought it was getting from GUIDE and related to button2 actually came from button 1. I can just see that having potential for problems so my method avoids the issue by not using them at all, which is fine because they're never needed anyway.
John Petersen
2012-11-6
That makes a lot of sense. Thanks!
Brad
2012-11-13
类别
在 帮助中心 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!