Clear Filters
Clear Filters

Calling a function from within a GUI (GUIDE)

13 views (last 30 days)
%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.

Accepted Answer

Image Analyst
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?

More Answers (1)

John Petersen
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
John Petersen
John Petersen on 6 Nov 2012
That makes a lot of sense. Thanks!
Brad
Brad on 13 Nov 2012
Thanks for this post. In the future, I will try creating a separate function that takes handles as an input and, if it made changes to handles, return it as an output so guidata() can later update the handles structure.
However, following Image Analyst's post on Nov 4 18:24, I cut the following code:
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
And replaced it with
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
That is, I got rid of the auto generated code and replaced it with the suggested switchyard style. The code now works fine, it simply required a lot of reworking of the code. Thank you again, everyone who contributed to this thread.

Sign in to comment.

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!