I'm trying to duplicate elements in the GUI, buttons to be specific.
1 view (last 30 days)
In my app I have a top row where the dimensions of a certain area are entered, how many buttons whould go in that area, and what the ratio of the width to height of the images should be. Then I'm having all the buttons appear on screen and they will be centered. I have no problem with finding the sizes and locations of the buttons, my main problem is where are all the buttons going to come from. My first though was to have one button that moved to all the positions and at each position make a clone or duplicate or even an afterimage of the button before moving it to the next location and doing the same until it reached the last location. Even just generating however many buttons are necessary is good with me, I could figure out how to get them all arranged. I just want to know a way to make a bunch of button appear(up to 100s) without having 100 buttons off screen at the beginning of the program. Thanks.
Dave B on 6 Aug 2022
If you're making your app in a uifigure (e.g. with app designer) you can create a button with uibutton, if you're making your app in a figure (e.g. with guide) you can create a button with uicontrol.
It's a lot easier with the uifigure-approach, because you can use a uigridlayout to lay out all your buttons (and other components). Here's a simple example (without app designer or guide):
% Top level grid
% A grid that holds the label and spinner where the user will choose how
% many buttons they want.
gridDataEntry = uigridlayout(grid);
gridDataEntry.ColumnWidth=["fit" "fit" "1x"];
% Components for UI
lbl=uilabel(gridDataEntry,"Text","How Many Buttons?");
spinButtonCount = uispinner(gridDataEntry,'Value',0);
% A grid to hold the buttons
gridButtons = uigridlayout(grid);
function buttoncallback(numbuttons, buttonparent)
% Delete all the existing buttons (or you'd have to compute the difference
% between the number of existing buttons and the number requested):
% If you don't specify, uigridlayout will just give you two columns of
buttonparent.ColumnWidth = repelem("fit",ceil(sqrt(numbuttons)));
buttonparent.RowHeight = repelem("fit",ceil(numbuttons/ceil(sqrt(numbuttons))));
for i = 1:numbuttons
% These buttons don't do anything!