Calling a function from within a GUI (GUIDE)
Mostrar comentarios más antiguos
%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.
Respuesta aceptada
Más respuestas (1)
John Petersen
el 1 de Nov. de 2012
1 voto
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 comentarios
Brad
el 1 de Nov. de 2012
Brad
el 2 de Nov. de 2012
Image Analyst
el 2 de Nov. de 2012
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
el 4 de Nov. de 2012
John Petersen
el 6 de Nov. de 2012
Editada: John Petersen
el 6 de Nov. de 2012
@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
el 6 de Nov. de 2012
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
el 6 de Nov. de 2012
That makes a lot of sense. Thanks!
Brad
el 13 de Nov. de 2012
Categorías
Más información sobre Interactive Control and Callbacks en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!