Clear Filters
Clear Filters

How can I generate selected number of tabs in tab group with app designer?

14 views (last 30 days)
I need to generate selected number of tabs within one tab group using app designer. Is there any way to do it?
My code:
app.TabGroup = uitabgroup(app.UIFigure);
app.TabGroup.Position = [300 300 300 300];
tab = uitab(app.TabGroup,'Title','Main Tab'); %create the main tab
for x = 1:5 %create the number of tabs entered by the user
tab.(['t' num2str(x)]) = uitab(app.TabGroup,'Title',['Tab' num2str(x)]);
end
But there is only one generated tab (figure below) and error "Unrecognized property 't1' for class 'matlab.ui.container.Tab'."
What can I do? Thanks.

Accepted Answer

Adam Danz
Adam Danz on 20 Mar 2020
Edited: Adam Danz on 20 Mar 2020
You're treating tab like a structure but it's a tab container object. Store the tab handles in a cell array. Here's a demo you can adapt to your app.
% Create a UI Figure
app.UIFigure = uifigure();
% Set up tab group
app.TabGroup = uitabgroup(app.UIFigure);
% app.TabGroup.Position = [300 300 300 300]; % not needed for demo
% Specify the total number of tabs
nTabs = 6; % including MainTab
% Set up the main tab
app.tabs = cell(1,nTabs);
app.tabs{1} = uitab(app.TabGroup,'Title','Main Tab'); %create the main tab
% Create additional tabs
for i = 2:nTabs %create the number of tabs entered by the user
app.tabs{i} = uitab(app.TabGroup,'Title',['Tab' num2str(i-1)]);
end
To access tab number 'n',
app.tabs{n}
app.tabs{n}.Title = 'Menu'; % for example
Note: when applying this to AppDesigner you'll need to declare tabs as a private property by following these instructions.
  7 Comments
Will Reeves
Will Reeves on 19 Aug 2022
Edited: Will Reeves on 19 Aug 2022
Actually... is it just OK to "copy" the "handle" to a local variable and delete it? It seems to work - as in the tab disappears from the GUI, but is it "clean"?
for i=1:length(app.tabs)
a=app.tabs{i};
delete(a);
end
Adam Danz
Adam Danz on 16 Sep 2022
@Will Reeves sorry for the delay, I was out on vacation for a while.
> is it just OK to "copy" the "handle" to a local variable and delete it
Yes. When you copy a variable containing a handle to another variable, you are making a semantic copy. Both variables are referencing the same object and if you make changes to properties using one variable, those properties will correctly be represented in the second variable.
For a deeper dive, see this blog post:

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!