Calling a function from within a GUI (GUIDE)

17 ビュー (過去 30 日間)
Brad
Brad 2012 年 10 月 31 日
%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.

採用された回答

Image Analyst
Image Analyst 2012 年 11 月 4 日
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?

その他の回答 (1 件)

John Petersen
John Petersen 2012 年 11 月 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 件のコメント
John Petersen
John Petersen 2012 年 11 月 6 日
That makes a lot of sense. Thanks!
Brad
Brad 2012 年 11 月 13 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by