Display message when two buttons are pushed?

2 views (last 30 days)
Rachel Loo
Rachel Loo on 23 Nov 2016
Commented: Rachel Loo on 23 Nov 2016
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!

Answers (2)

Walter Roberson
Walter Roberson on 23 Nov 2016
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 Comment
Rachel Loo
Rachel Loo on 23 Nov 2016
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

Sign in to comment.


Image Analyst
Image Analyst on 23 Nov 2016
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 Comment
Rachel Loo
Rachel Loo on 23 Nov 2016
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

Sign in to comment.

Categories

Find more on Develop Apps Using App Designer 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!