MATLAB Answers

Ruger28
0

Looking to generate an array (grid) of pushbuttons in a figure

Asked by Ruger28
on 17 Apr 2019
Latest activity Edited by Geoff Hayes
on 18 Apr 2019
Hello All,
I am trying to generate a grid of pushbuttons in a figure. I will prompt the user, and get a numeric value of the grid size, and then generate said grid. For example, if the user enters 25, the script will generate 25 push buttons in a 5x5 grid. The user will then select one or more of these buttons, and then once another button ('GO' for example) is pressed, I will get the values of the numbers in the grid to use for another purpose. Anyone know how the best way to go about this? Searching hasnt gotten me too far, so any help would be greatly appreciated.
Also, I am using Matlab 2015b.
Thanks!
-J

  4 Comments

Show 1 older comment
Thanks for the info, but I am using matlab 2015b, and cannot use App Designer.
Ruger28 - is there a maximum number of pushbuttons allowed? Have you created a GUI in GUIDE that prompts the user for the number of buttons? Would the 5x5 grid of buttons be contained within that GUI or a separate one?
Geoff - There is, but that isnt the part I am concerned about. Let's just say 25 is the value it will always be. I found I was able to generate this using this code:
N = 25;
N = sqrt(N);
hFig = figure;
buttonSize = 0.05;
hGroup = uibuttongroup('Units','Normalized','Position',[0 0 1 1]);
% [left bottom width height
xpos = 0; ypos = 0; jj = 1;
for i = 1:N*N
if mod(i,5) == 1 && i~=1
xpos = xpos + buttonSize;
jj = 1;
end
ypos = (jj-1)*buttonSize;
hText(i) = uicontrol('Style','pushbutton','String',[num2str(i)],...
'Parent',hGroup,'Units','normalized','Position',[xpos ypos .05 .05],...
'BackgroundColor','white','Callback',{@pushbutton_callback,kk});
jj = jj + 1;
end
This generates the grid of buttons I am looking for. The only issue now is that I am trying to get a values of which buttons are pressed. Apparently, callbacks dont ouput data, so I am stuck on that.

Sign in to comment.

1 Answer

Answer by Geoff Hayes
on 18 Apr 2019
Edited by Geoff Hayes
on 18 Apr 2019
 Accepted Answer

Ruger28 - you can get the string value of the button from within the callback. Just remove the input (kk?) as an input parameter to the callback
hText(i) = uicontrol('Style','pushbutton','String',[num2str(i)],...
'Parent',hGroup,'Units','normalized','Position',[xpos ypos .05 .05],...
'BackgroundColor','white','Callback',{@pushbutton_callback});
and your callback would just be
function pushbutton_callback(hObject, hEvent)
id = str2double(get(hObject,'String'));
where id is the number on the button.
You're right, the callback won't output anything but if you nest your callback within a parent function then perhaps you can update a variable declared in the parent function that can then be used elsewhere...or if you are using GUIDE, then you can probably update the handles structure so that other callbacks in your GUI have access to it.

  1 Comment

Geoff - Thank you, sir! I appreciate the help.

Sign in to comment.