How to change automatically generated uibutton properties in another function?

1 view (last 30 days)
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 Comment
sid
sid on 23 Nov 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

Sign in to comment.

Accepted Answer

Mohammad Sami
Mohammad Sami on 22 Nov 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 Comments
Mohammad Sami
Mohammad Sami on 23 Nov 2021
How did you access the fontname property. You need to but the index before the .FontName.
app.orientationButtons(b).FontName = 'Arial';
sid
sid on 24 Nov 2021
resolved. cant get your solution to work, but the uigridlayout is a good idea. thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!