How to change automatically generated uibutton properties in another function?

1 visualización (últimos 30 días)
The following code is run at startup that generated buttons based on a number of unique files in a folder. The callbackfcn is another function, where the user would choose one of these buttons. At that point, i'd like to highlight the chosen button, which is easy, but i would also like to grey out, or delete the other buttons. The qquestion is how to I access the other buttons that were generated?! I can get to the button that was clicked easily, just not the other ones.
%
% make buttons
numButtons = max(size(app.orientationUnique));
for b = 1:numButtons
posidx = b*(30+app.TheHuntGUI.Position(1));
app.orientationButtons = uibutton(app.TheHuntGUI, 'push');
app.orientationButtons.FontName = 'Arial';
app.orientationButtons.FontSize = 18;
app.orientationButtons.FontWeight = 'bold';
app.orientationButtons.Position = [posidx 500 120 40];
app.orientationButtons.Text = app.orientationUnique{b};
app.orientationButtons.Tag = app.orientationUnique{b};
app.orientationButtons.ButtonPushedFcn = createCallbackFcn(app, @orientationButtonPushed, true);
end
  1 comentario
sid
sid el 23 de Nov. de 2021
Resolved.
numButtons = max(size(app.orientationUnique));
for b = 1:numButtons
posidx = b*(30+app.TheHuntGUI.Position(1));
app.orientationButtons(b) = uibutton(app.TheHuntGUI, 'push');
set(app.orientationButtons(b),'FontName','Arial');
set(app.orientationButtons(b),'FontSize',18);
set(app.orientationButtons(b),'FontWeight','bold');
set(app.orientationButtons(b),'Position',[posidx 500 120 40]);
set(app.orientationButtons(b),'Text', app.orientationUnique{b});
set(app.orientationButtons(b),'Tag',app.orientationUnique{b});
set(app.orientationButtons(b),'ButtonPushedFcn',createCallbackFcn(app, @orientationButtonPushed, true));
end

Iniciar sesión para comentar.

Respuesta aceptada

Mohammad Sami
Mohammad Sami el 22 de Nov. de 2021
You can store all your buttons as an array in the app property orientationButtons.
function create_buttons(app)
% % make buttons
numButtons = max(size(app.orientationUnique));
for b = 1:numButtons
posidx = b*(30+app.TheHuntGUI.Position(1));
app.orientationButtons(b) = uibutton(app.TheHuntGUI, 'push');
app.orientationButtons(b).FontName = 'Arial';
app.orientationButtons(b).FontSize = 18;
app.orientationButtons(b).FontWeight = 'bold';
app.orientationButtons(b).Position = [posidx 500 120 40];
app.orientationButtons(b).Text = app.orientationUnique{b};
app.orientationButtons(b).Tag = app.orientationUnique{b};
app.orientationButtons(b).ButtonPushedFcn = createCallbackFcn(app, @orientationButtonPushed, true);
end
end
function orientationButtonPushed(app,event)
btnclicked = event.Source;
allotherbtns = app.orientationButtons(~ismember(app.orientationButtons,btnclicked));
end
Also I suggest you use uigridlayout as the parent for your buttons. This will automatically adjust the sizes of the button based on the available space instead of manually setting the positions in code.
  3 comentarios
Mohammad Sami
Mohammad Sami el 23 de Nov. de 2021
How did you access the fontname property. You need to but the index before the .FontName.
app.orientationButtons(b).FontName = 'Arial';
sid
sid el 24 de Nov. de 2021
resolved. cant get your solution to work, but the uigridlayout is a good idea. thank you.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by