PLEASE HELP!! Matlab AppDesigner Putting a certain number of panels

4 views (last 30 days)
Hello,
I have a problem with matlab app designer. I want to do something; when I select the amount in the list box and then click the button, that amount of panels are shown. How can I do that? I wrote function to get able to visibility of panel after push the button. But I cannot define the number of panels.
Thanks for help!
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.Panel.Visible = "off";
end
% Callback function
function ButtonGroupSelectionChange(app, event)
end
% Button pushed function: Button
function ButtonPushed(app, event)
app.Panel.Visible="on";
end
end
  3 Comments
Georgiana Cristea
Georgiana Cristea on 19 Oct 2021
Hello!
I am currently trying to handle the same issue, and as an answer to your question, I would like the panels to be shown on top of each other and then add some additional two buttons to help me switch through them.
Please respond asap.
Thank you in advance for the help!
Kind Regards,
G
Yongjian Feng
Yongjian Feng on 24 Oct 2021
Why not tabs? Then you don't need to worry about how to position them, and what happens if there are too many.

Sign in to comment.

Answers (1)

Vedant
Vedant on 20 Feb 2025
Assuming that the desired output should have the panels stacked on top of each other, you can dynamically add the selected number of panels from the list box when the button is clicked by modifying the callback function for the button. A dropdown menu is provided for navigation between the panels, as adding panels separately would cause them to overflow out of the UI if their number exceeds a certain limit. Below is the code that achieves this functionality:
First, define three private properties to manage the panels and the navigation bar:
properties (Access = private)
PanelTemplate matlab.ui.container.Panel % A sample Panel
DynamicPanels matlab.ui.container.Panel % Array to hold panels
NavigationBar matlab.ui.control.DropDown %Navigation menu
end
Implement the navigateToPanel function, which will serve as a callback for the NavigationBar:
methods (Access = public)
function navigateToPanel(app, selectedPanel)
% Extract the panel index from the selected item
panelIndex = str2double(extractAfter(selectedPanel, 'Panel '));
if ~isempty(panelIndex) && panelIndex >= 1 && panelIndex <= length(app.DynamicPanels)
% Hide all panels
for i = 1:length(app.DynamicPanels)
app.DynamicPanels(i).Visible = 'off';
end
% Show the selected panel
app.DynamicPanels(panelIndex).Visible = 'on';
else
% Handle invalid selection
disp('Invalid panel selection.');
end
end
end
Add a callback function for the button to implement the desired functionality:
function ButtonPushed(app, event)
% Define the position for the panels
startX = 250; % X position
startY = 300; % Y position for all panels
panelWidth = 200;
panelHeight = 50;
% Create a template panel (initially hidden)
app.PanelTemplate = uipanel(app.UIFigure);
app.PanelTemplate.Title = 'Panel';
app.PanelTemplate.Position = [startX, startY, panelWidth, panelHeight];
app.PanelTemplate.Visible = 'off'; % Initially hidden
if ~isempty(app.DynamicPanels)
delete(app.DynamicPanels);
end
% Reset the DynamicPanels to an empty array
app.DynamicPanels = matlab.ui.container.Panel.empty;
% Get the number of panels to create from ListBox selection
numPanels = str2double(app.ListBox.Value);
% Create dynamic panels
for i = 1:numPanels
% Create a new panel based on the template
newPanel = copy(app.PanelTemplate);
newPanel.Visible = "on";
newPanel.Position = [startX, startY, panelWidth, panelHeight]; % Overlapping position
newPanel.Title = ['Panel ' num2str(i)];
newPanel.Parent = app.UIFigure; % Add to the figure
% Append the new panel to the DynamicPanels array
app.DynamicPanels(end+1) = newPanel;
end
% Create a dropdown menu for navigation
if isfield(app, 'NavigationBar') && isvalid(app.NavigationBar)
delete(app.NavigationBar);
end
app.NavigationBar = uidropdown(app.UIFigure);
app.NavigationBar.Position = [startX + panelWidth + 20, startY, 100, 22];
app.NavigationBar.Items = arrayfun(@(x) ['Panel ' num2str(x)], 1:numPanels, 'UniformOutput', false);
% Set default selection to the last panel
if numPanels > 0
app.NavigationBar.Value = app.NavigationBar.Items{end};
end
app.NavigationBar.ValueChangedFcn = @(src, event) app.navigateToPanel(src.Value);
end
This implementation ensures that the app functions as intended. Below is a screenshot demonstrating the app's functionality:

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!