How to determine if pushbutton/callback is pressed?

14 ビュー (過去 30 日間)
Jan Michael Manalac
Jan Michael Manalac 2016 年 6 月 5 日
コメント済み: Image Analyst 2018 年 2 月 20 日
How do I determine if a callback was pressed in guide GUI?
Say I have 2 Callbacks and
If I press the first Callback, then
function first_Callback(hObject, ..., ...)
var1 = 1; % callback was pressed
function second_Callback(hObject, ..., ...)
if(var1 == 1)
%code block
Thank You!

回答 (2 件)

Shameer Parmar
Shameer Parmar 2016 年 6 月 10 日
Hi Jan,
Try for this:
function pushbutton1_Callback(hObject, eventdata, handles)
handles.First = 1;
handles.Second = 0;
clc;
disp('Fist Button was pressed.');
guidata(hObject, handles);
.
function pushbutton2_Callback(hObject, eventdata, handles)
handles.First = 0;
handles.Second = 1;
clc;
disp('Second Button was pressed.');
guidata(hObject, handles);
Once you add this logic and click on respective push button, the two variables handles.First and handles.Second will be created and assign with either 0 or 1 value. You can then use it in your logic to check which push button was pressed.
if (handles.First==1 && handles.Second==0)
disp('Fist Button was pressed.');
elseif (handles.First==0 && handles.Second==1)
disp('Second Button was pressed.');
end
  2 件のコメント
Thanigaivel Raja T
Thanigaivel Raja T 2016 年 10 月 31 日
編集済み: Thanigaivel Raja T 2016 年 10 月 31 日
If both push buttons are not pressed, then handles.First will not have been defined. Then error message will be displayed by MATLAB.
Image Analyst
Image Analyst 2016 年 10 月 31 日
This does not describe Shameer's code. His code defines handles.First even if only one pushbutton is pressed. So it's not "both" as you said. The only problem would be is if "neither" is pressed. So to avoid that, you should put the following code in the OpeningFcn() function
% Indicate that neither pushbutton has been clicked yet:
handles.First = 0;
handles.Second = 0;

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


Image Analyst
Image Analyst 2016 年 6 月 5 日
Simply set a breakpoint at the first line of code in the callback. It should stop there if you interact with the GUI control.
If you don't know how to do that, please see this link, which will solve nearly every problem anyone might ever have in MATLAB.
  2 件のコメント
Ram
Ram 2018 年 2 月 20 日
Image Analyst
Image Analyst 2018 年 2 月 20 日
Alternatively, as the first two lines of the callback you can have this:
fprintf('Now in the pushbutton callback.\n');
uiwait(helpdlg('Now in the pushbutton callback.\n'));

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

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by