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

8 visualizzazioni (ultimi 30 giorni)
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.

Risposte (1)

Dave B
Dave B il 6 Ago 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
grid.RowHeight=["fit" "1x"];
% 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
% 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!


Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by