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

60 vues (au cours des 30 derniers jours)
Ruger28
Ruger28 le 17 Avr 2019
Modifié(e) : Geoff Hayes le 18 Avr 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 commentaires
Geoff Hayes
Geoff Hayes le 18 Avr 2019
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?
Ruger28
Ruger28 le 18 Avr 2019
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.

Connectez-vous pour commenter.

Réponse acceptée

Geoff Hayes
Geoff Hayes le 18 Avr 2019
Modifié(e) : Geoff Hayes le 18 Avr 2019
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 commentaire
Ruger28
Ruger28 le 18 Avr 2019
Geoff - Thank you, sir! I appreciate the help.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by