Calling a function from within a GUI (GUIDE)
9 views (last 30 days)
Show older comments
%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.
0 Comments
Accepted Answer
Image Analyst
on 4 Nov 2012
Regarding your last comment: Who cares? There is a lot of boilerplate code, especially near the beginning of the m-file. Just don't worry about it. Like I said, ignore eventdata and hObject just like you ignore all the boilerplate code at the top of the file. And who cares if you can't put code in the individual callback for a particular radio button. You can do it in the group callback, like the code I gave you:
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
No, you're not "the first to have auto generated a buttongroup in GUIDE and then needed to call it from a pushbutton." I do it all the time using code like above. It works, so I don't want to get into a philosophical discussion with them about why it should, perhaps, be done in separate, individual callbacks.
When you say " Surely there is some simple work around.", why don't you consider putting your code in the group callback a workaround, or an acceptable workaround? It doesn't do it the way you think it should (separate, individual callbacks), and you don't like the group callback workaround. What kind of other workaround would you like that would be better?
0 Comments
More Answers (1)
John Petersen
on 1 Nov 2012
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 Comments
See Also
Categories
Find more on Interactive Control and Callbacks 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!