- Switch back to the previous tab (set the SelectedTab back to the previous tab).
- Run your code.
- Switch back to the new tab.
- Record the current tab, for use in step 1 next time.
TabGroup: How to execute code before selection changed?
2 views (last 30 days)
Show older comments
Tina Wunderlich
on 6 Jan 2022
Commented: Tina Wunderlich
on 7 Jan 2022
Hi,
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!
0 Comments
Accepted Answer
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:
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;
end
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));
pause(1);
title(a,'');
tg.UserData.PreviousTab = newTab;
tg.SelectedTab = newTab;
end
end
More Answers (0)
See Also
Categories
Find more on Dialog Boxes 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!