Why is TabGroup.S​electedTab​.Title giving me the wrong title?

5 views (last 30 days)
I am simulating a processing system with groups of equipment broken up into different 'workcenters', where each workcenter is allowed a different operating schedule. In an app, built in app designer, each workcenter's equipment are displayed in a table, and each equipment table is displayed on a different tab of a single tab group.
If a user chooses to add equipment to the system, it is essential that I know what workcenter to add it to.
Problem: I have been using the TabGroup.SelectedTab.Title property to do this, but in 2023b Update 7, I noticed when you query this property, it does not reliably return the title of the tab that is actually selected.
I reported this to MathWorks Technical Support, and the person I spoke to said this issue had not been reported yet.
Any ideas?
Kind regards,
David
1) mouse location? but this method would depend on the size and shape of the title text.
I'll likely need to rearrange my content into different containers, but it will take time.

Answers (1)

David Meissner
David Meissner on 29 Feb 2024
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

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!