properties (Access = private)
Tab2 % Create premade property names with common format.
Tab3
Label2
Label3
idx = 1 % My app starts with just one tab, and that user adds more.
end
methods (Access = private)
function getInfo(app, ~, evt)
app.TitleEditField.Value = evt.Source.Title; % Display correct title.
app.NumberEditField.Value = evt.Source.UserData; % Display correct index.
app.idx = evt.Source.UserData; % Store for later.
end
end
methods (Access = public)
function createTab(app)
n = length(app.TabGroup.Children) + 1; % Tab number
if n > 3; uialert(app.UIFigure, "No more tabs.","Alert"); return; end % For this example
ns = num2str(n);
% Create tab with the premade callback.
app.("Tab" + ns) = uitab(app.TabGroup, 'Title', "Tab" + ns,...
'ButtonDownFcn', @(src, evt) app.getInfo(src, evt), 'UserData', n);
% Create some type of data container.
app.("Label" + ns) = uilabel(app.("Tab" + ns),...
"Text", ns, "Position", app.Label1.Position, "FontSize", 48);
end
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.TitleEditField.Value = app.TabGroup.SelectedTab.Title; % First tab.
% Set callback and userdata for the first tab's buttondown function.
app.TabGroup.Children(1).ButtonDownFcn = @(src, evt) app.getInfo(src, evt);
app.TabGroup.Children(1).UserData = 1;
end
% Button pushed function: MakeTabButton
function MakeTabButtonPushed(app, event)
createTab(app)
end
% Button pushed function: getdataButton
function getdataButtonPushed(app, event)
% Get data from the chosen container.
app.DataEditField.Value = app.TabGroup.Children(app.idx).Children(1).Text;
end
end
This technique might work, but the idea is basically store an index associated with each tab in the tab's UserData (when its created) and then grab that from the event info when the user selects a tab. Then use that index to grab the right tab to get into the data stored on that tab. This method does not require strcmp to find the right tab. Note the first tab's callback is specified in the startupFcn.
Final results: TBD