MATLAB: How to set the SelectionChangedFcn for the tabs group I have created

selectionchangedfcn for tabssend arguments into a callbacktab selection changetabs with guide

Hello , I'm doing a master degree in physics and I have the need to program a GUI to do certain theoretical calculations and plot graphs. In this GUI there are 3 tabs, I need to be able to know when the selected tab is changed. There is a SelectionChangedFcn when a tab group is created… Well… first of all this is the initial code that is executed before the GUI is made visible:
% Create tab group
handles.tgroup = uitabgroup('Parent', handles.figure1,'TabLocation', 'top');
handles.tab1 = uitab('Parent', handles.tgroup, 'Title', 'Materials and Optics selection');
handles.tab2 = uitab('Parent', handles.tgroup, 'Title', 'Dispersion graphs');
handles.tab3 = uitab('Parent', handles.tgroup, 'Title', 'My Tab Label 3');
%Create a handle for the selection change function of the tab group
tgroup.SelectionChangedFcn = @tabChangedCB;
% Place panels into each tab
set(handles.uipanel1,'Parent',handles.tab1)
set(handles.uipanel2,'Parent',handles.tab2)
set(handles.uipanel3,'Parent',handles.tab3)
% Reposition each panel to same location as panel 1
set(handles.uipanel2,'position',get(handles.uipanel1,'position'));
set(handles.uipanel3,'position',get(handles.uipanel1,'position'));
In the first lines the tab group is created, I have noticed that when using guide all the objects appear in the object browser… BUT tgroup DOES NOT appear in the object browser… so I cannot set the SelectionChangedFcn in the property inspector of tgroup because I cannot browse it… Does anybody have any idea how this can be done..??

Best Answer

By writting the code like this:
%Setting the uipanel1 height, width and position as figure1
handles.uipanel1.Position = [0 0 handles.figure1.Position(3) handles.figure1.Position(4)];
%This line of code has to come before creating the tab group, otherwise the
%tabs won't have the same width height and position as figure 1, only the
%first one will.
% Create tab group with SelectionChangedFcn
handles.tgroup = uitabgroup('Parent', handles.figure1,'TabLocation', 'top','SelectionChangedFcn',...
@(hObject,eventdata)Frogsim('tgroup_SelectionChangedFcn',hObject,eventdata,guidata(hObject)));
% This line tells the uitabgroup function that the name of the callback
% function for the selection change is named tgroup_SelectionChangedFcn
% , and also sends in the input argument guidata(hObject) which is the
% updated structure of handles and data , the eventdata is sent
% automatically.
handles.tab1 = uitab('Parent', handles.tgroup, 'Title', 'Materials and Optics selection');
handles.tab2 = uitab('Parent', handles.tgroup, 'Title', 'Dispersion graphs');
handles.tab3 = uitab('Parent', handles.tgroup, 'Title', 'My Tab Label 3');
% Place panels into each tab
set(handles.uipanel1,'Parent',handles.tab1)
set(handles.uipanel2,'Parent',handles.tab2)
set(handles.uipanel3,'Parent',handles.tab3)
% Reposition each panel to same location as panel 1
set(handles.uipanel2,'position',get(handles.uipanel1,'position'));
set(handles.uipanel3,'position',get(handles.uipanel1,'position'));
Then the SelectionChangedFcn callback for the tab group called tgroup is set correctly. In this case my gui is called Frogsim , if your gui is called mygui then the third line should look like this:
@(hObject,eventdata)mygui('tgroup_SelectionChangedFcn',hObject,eventdata,guidata(hObject)));
Later in the code I wrote the SelectionChangedFcn for tgroup like this:
% --- Executes on selection change of tabs in tgroup tab group.
function tgroup_SelectionChangedFcn(hObject, eventdata, handles)
% hObject handle to tgroup (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
And then you can add whatever code you want to the selection change function.
To explain better what the third line did, the line of code:
@(hObject,eventdata)Frogsim('tgroup_SelectionChangedFcn',hObject,eventdata,guidata(hObject)));
creates an anonymous function that accepts the arguments
hObject
eventdata
then the anonymous function calls the function:
Frogsim('tgroup_SelectionChangedFcn',hObject,eventdata,guidata(hObject)));
what that does is to call the tgroup_SelectionChangedFcn function within Frogsim with arguments hObject and eventdata (That were passed inside by the anonymous function) and then the 3rd input argument is guidata(hObject) which will give the updated structure of handles and user data in a guide built gui, and that corresponds to the 3rd arguments of SelectionChangedFcn which is
handles