TabGroup: How to execute code before selection changed?

3 views (last 30 days)
I've written a GUI with a TabGroup that has five tabs. If the user makes some changes to data on Tab1 and then changes to Tab2 I would like to execute some code before the Tab changes. I've added a SelectionChanged callback to my TabGroup and tried to put the code to be executed there, but then it is executed after the Tab changed. So I would like to have following order: (1) user clicks on new tab, (2) execute some code, (3) change to new selected tab.
I'm using Matlab R2020b.
Thank you very much!

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 7 Jan 2022
Edited: Benjamin Kraus on 7 Jan 2022
I'm curious, why do you need the code to run before switching tabs?
I don't believe this is possible without basically implementing your own tabs. For example, you could use buttons above a stack of uipanels, and when users click the buttons you can make different uipanels visible/invisible (but run your own code first). I suppose you could also attempt to overlay buttons on-top-of the tabs (to hide the tabs), but that would probably be even clunkier than the button + uipanel approach.
Maybe another alternative would be to have your SelectionChanged callback do the following:
  1. Switch back to the previous tab (set the SelectedTab back to the previous tab).
  2. Run your code.
  3. Switch back to the new tab.
  4. Record the current tab, for use in step 1 next time.
This will result in a flicker (you will briefly see the new tab before switching back to the old tab), but it is the best I can think of, and it might work well enough. For example:
tg = uitabgroup;
t(1) = uitab(tg,'Title','one');
t(2) = uitab(tg,'Title','two');
t(3) = uitab(tg,'Title','three');
for ii = 1:3
ax(ii) = axes(t(ii));
ax(ii).Color(ii) = 0;
tg.UserData.PreviousTab = t(1);
tg.SelectionChangedFcn = @change;
function change(tg, ~)
newTab = tg.SelectedTab;
previousTab = tg.UserData.PreviousTab;
if newTab ~= previousTab % This should prevent recursion.
tg.SelectedTab = tg.UserData.PreviousTab;
a = previousTab.Children;
title(a,sprintf('Goodbye %s', previousTab.Title));
tg.UserData.PreviousTab = newTab;
tg.SelectedTab = newTab;
  1 Comment
Tina Wunderlich
Tina Wunderlich on 7 Jan 2022
Hi Benjamin,
thank you very much for this fast and clear answer! I just implemented your suggestion and its fine for me!
Just to answer your question why I need this: On Tab1 the user can make some changes to data and the changes are saved in a log file. When the user finishes editing and is changing to another tab I would like to automatically send an email with the changes and open a message box that an email was send (and the user has to click OK). I would like this message box to appear in front of Tab1 and not in front of the new tab. So its more a "nice to have" thing than really essential.

Sign in to comment.

More Answers (0)


Find more on Startup and Shutdown 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!