MATLAB: In GUI when we have a tabgroup how to change the current tab by pressing a bottom

change the current uitabmatlab guiuitabuitabgroup

Hello, I'm a beginner in Matlab GUI, I have a tabgroup like bellow code and a push button, I don't know how to code the callbacks in order to change tab by pressing the push button.
unction Main_TabbedTest
clc; clear; close all;
%% Base figure
S.fig = figure('NumberTitle','off', 'Name', 'Main_TabbedTest', ...
'MenuBar','none', 'ToolBar','none', 'Units','Norm', ...
'Position',[.2 .2 .6 .6], 'Resize','off', 'Color',.94*[1 1 1]);
%% Tabs
S.tb = uitabgroup('unit','norm', 'pos',[0 0 1 1]);%[0 .3 1 .7]
S.tab1 = uitab('parent',S.tb, 'title',' Tab 1 ');
S.tab2 = uitab('parent',S.tb, 'title',' Tab 2 ');
FS=14;
%======== 'Pushbutton'==========
S.Apply = uicontrol('parent',S.tab1,'style','pushbutton', 'unit','norm', ...
'pos',[0.65 0.23 0.13 0.08],'String','Next tab','fontname','Cambria','fontsize',FS,'TooltipString','Push to aplly properties ',...
'Callback',@buttonApply );

Best Answer

Hi Amir,
Try the code below. I have commented some lines.
classdef Main_TabbedTest < handle
% you should create a handle class to pass object reference into
% callback functions
properties
S % struct property to store tab, button items
end
methods
%Constructor method
function tabTestObj = Main_TabbedTest
%% Base figure
tabTestObj.S.fig = figure('NumberTitle','off', 'Name', 'Main_TabbedTest', ...
'MenuBar','none', 'ToolBar','none', 'Units','Norm', ...
'Position',[.2 .2 .6 .6], 'Resize','off', 'Color',.94*[1 1 1]);
%% Tabs
tabTestObj.S.tb = uitabgroup('unit','norm', 'pos',[0 0 1 1]);%[0 .3 1 .7]
tabTestObj.S.tab1 = uitab('parent',tabTestObj.S.tb, 'title',' Tab 1 ');
tabTestObj.S.tab2 = uitab('parent',tabTestObj.S.tb, 'title',' Tab 2 ');
FS=14;
%======== 'Pushbutton'==========
S.Apply = uicontrol('parent',tabTestObj.S.tab1,'style','pushbutton', 'unit','norm', ...
'pos',[0.65 0.23 0.13 0.08],'String','Next tab','fontname','Cambria','fontsize',FS,'TooltipString','Push to aplly properties ',...
'Callback',@tabTestObj.buttonApply );
end
% Apply buton callback
function buttonApply(obj, handle,evntdata)
% obj: Main_TabbedTest object
% handle: button object handle
% eventdata: action data
% S.tb is stored as tab group SelectedTab is a property of
% tabgroup stores selected tab object. you can assign tab object as follows:
%% OPTION 1
set(obj.S.tb, 'SelectedTab',obj.S.tab2);
%% OPTION 2
% if you want to do it as numeric obj.S.tb.Children includes
% 2x1 tab objects array select the second one or first one:
% obj.S.tb.SelectedTab = obj.S.tb.Children(2); ... comment option1 and uncomment
% and try this
end
end
end