I'm trying to duplicate elements in the GUI, buttons to be specific.

9 views (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.

Answers (1)

Dave B
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):
f=uifigure;
% Top level grid
grid=uigridlayout(f);
grid.RowHeight=["fit" "1x"];
grid.ColumnWidth="1x";
% A grid that holds the label and spinner where the user will choose how
% many buttons they want.
gridDataEntry = uigridlayout(grid);
gridDataEntry.RowHeight="1x";
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);
spinButtonCount.ValueChangedFcn=@(~,~)buttoncallback(spinButtonCount.Value,gridButtons);
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):
delete(buttonparent.Children)
% If you don't specify, uigridlayout will just give you two columns of
% buttons.
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!
uibutton(buttonparent);
end
end

Categories

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

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!