How to reference random GUI buttons?

3 views (last 30 days)
I am trying to create a connect 4 sort of game. When the user selects a button in the GUI I would like a random other button to be pressed as well but in a different color. I thought of using the randi function since all of my buttons are named as "pushbutton#" for (1-42) but I can't figure out how to incorporate that into my code. Thanks all suggestions are welcome.

Accepted Answer

Nick Counts
Nick Counts on 4 Nov 2016
There are a bunch of ways you could approach this problem - here are some possible approaches:
Method 1
The first approach doesn't require you to hold onto any variables or handles. When you create your buttons, you need to set the 'tag' property to something unique for each button. Then you use findobj to grab the button you want.
buttonHandle = findobj(gameFigure, 'tag', 'button_1');
The trick to this approach is being sure you tag the button uicontrols properly. Stick to your naming convention and then you generate a random number and build the 'tag' based on your convention:
randButton = randi(42,1,1);
findobj(gameFigure, 'tag', sprintf('button_%d', randButton) )
Method 2
The other approaches require you to keep track of handles to your uicontrol objects (the buttons).
For this approach, I recommend generating your game board programmatically. Then, as you create the buttons, you save the handle to an array. Something like:
gameFigure = figure;
numButtons = 42;
buttonHandleArray = [];
% Generate all of your buttons and store them in an array of uicontrol
% handles.
for i = 1:numButtons
% You will need to add code to set the 'position' property, of course
buttonHandle = uicontrol(gameFigure, 'Style', 'pushbutton');
buttonHandleArray = vertcat(buttonHandleArray, buttonHandle);
end
% Pick a random button
randomButton = randi(10, 1, 1);
% Access that button from your array of uicontrol handles and set a
% property
buttonHandleArray(randomButton).BackgroundColor = [1 0 0];
Method 3
Similar to above, you can save all your uicontrol handles in a structure. This is similar to the handles structure that GUIDE creates.
You could even name your buttons in GUIDE by setting the 'tag' property. For example, add a button and set its 'tag' to 'button_1'
Then you can access it from callbacks with handles.button_1
One more fun thing: you can access structure fields dynamically with the syntax structure.(fieldNameExpression)
This can be pretty powerful in many contexts, although it might not make sense here. I still think it's fun to share:
hs = struct;
hs.fig = figure;
numButtons = 42;
for i = 1:numButtons
% Generate a field name string
dynamicFieldNameString = sprintf('button_%d',i)
% Assign the uicontrol handle to the field name you just made!
hs.(dynamicFieldNameString) = uicontrol('Style', 'pushbutton');
end
% Pick a random button
randomButton = randi(10, 1, 1);
% Access the button handle through dynamic field names
hs.(sprintf('button_%d', i))

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!