Display message when two buttons are pushed?

조회 수: 2 (최근 30일)
Rachel Loo
Rachel Loo 2016년 11월 23일
댓글: Rachel Loo 2016년 11월 23일
Hi I'm trying to design a matching game where if the correct two check boxes are checked, a message will appear. I already know I can use msgbox(text). My problem is determining when the correct two boxes are checked.
c1 = uicheckbox(fig,'Text','',...
'Value',0,...
'Position',[115,150,200,70],...
'ValueChangedFcn',@(c1,event) c1Changed(c1));
c2 = uicheckbox(fig,'Text','',...
'Value',0,...
'Position',[335,150,200,70],...
'ValueChangedFcn',@(c2,event) c2Changed(c2));
function c1Changed(c1)
c1Val = c1.Value;
end
function c2Changed(c2)
c2Val = c2.Value;
end
Thank you in advance!

답변 (2개)

Walter Roberson
Walter Roberson 2016년 11월 23일
Run through all of your checkboxes that are involved, getting their Value properties -- which you could probably do in a single step by using get() with a vector of handles.
Then use the vector of Values to figure out if a match had occurred.
  댓글 수: 1
Rachel Loo
Rachel Loo 2016년 11월 23일
I wasn't sure how to incorporate the vector, but I did try to use get(). However, it says my handles variable is undefined:
function testCompChecks()
fig=uifigure('Name','Matching Game','Position',[0 40 1450 840]);
c1 = uicheckbox(fig,'Text','',...
'Value',0,...
'Position',[115,150,200,70],...
'ValueChangedFcn',@(c1,event) c1Changed(c1));
c2 = uicheckbox(fig,'Text','',...
'Value',0,...
'Position',[335,150,200,70],...
'ValueChangedFcn',@(c2,event) c2Changed(c2));
handles = guihandles(fig);
function c1Changed(c1)
c1Val = c1.Value;
handles.c1 = c1Val;
guidata(fig,handles);
end
function c2Changed(c2)
c2Val = c2.Value;
handles.c2 = c2Val;
guidata(fig,handles);
end
function bothChecked_Callback()
get(handles.c1,'Value',handles.c2,'Value');
if (handles.c1 == handles.c2)
msgbox('You''ve found a match!');
end
end
end

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2016년 11월 23일
You don't need the function bothChecked_Callback(). And you don't need to have any code in the callbacks for the checkboxes like this:
c1Val = c1.Value; % Totally unnecessary
handles.c1 = c1Val; % Totally unnecessary
The value is ALREADY stored in the handles array. No need to make yet another field to hold the value that is already there.
Whenever you need to determine whether both checkboxes are checked, in whatever callback or function you want that has access to handles, simply do this:
if handles.c1.Value && handles.c2.Value
% Both are checked
msgbox('You found a match!');
else
% At least one is not checked.
end
This is a much simpler option and eliminates a lot of unneeded/unnecessary code and functions. You can put that code anywhere, like in the checkbox callbacks, or pushbutton callbacks, or any custom (non-callback) function that you pass handles to. Don't create new fields and functions that just duplicates functionality that is already there!
  댓글 수: 1
Rachel Loo
Rachel Loo 2016년 11월 23일
I added a callback function, but it says "there is no callback property on the checkbox class?:
I included a callback function in the c1 checkbox:
c1 = uicheckbox(fig,'Text','',...
'Value',0,...
'Position',[115,150,200,70],...
'Callback',@boxesChecked_callback);
and included a callback function for the if statement you said:
function boxesChecked_callback(hObject, eventdata, handles)
handles = guihandles(fig);
if (handles.c1.Value && handles.c2.Value)
% Both are checked
msgbox('You found a match!');
else
% At least one is not checked.
end
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by