Display message when two buttons are pushed?
2 views (last 30 days)
Show older comments
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!
0 Comments
Answers (2)
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.
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!
See Also
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!