Borrar filtros
Borrar filtros

How do I set the SelectionChangedFcn for the tabs group I have created?

17 visualizaciones (últimos 30 días)
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..??

Respuesta aceptada

test run
test run el 19 de Sept. de 2016
Editada: test run el 19 de Sept. de 2016
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
  1 comentario
Adam
Adam el 19 de Sept. de 2016
That is interesting and something I have never actually tried. I would perhaps have thought that when a function is included, like 'guidata(hObject)' rather than as a function handle, that the function would be evaluated there and then in the code, but I haven't tried it so if it works and is re-evaluated every time the callback is triggered rather than just once then this is definitely neat.

Iniciar sesión para comentar.

Más respuestas (1)

Adam
Adam el 19 de Sept. de 2016
Editada: Adam el 19 de Sept. de 2016
I was posting this in response to your answer which you deleted while I was typing!, but hopefully it still makes sense by itself...what you were doing is fine apart from this change as far as I could see.
Don't pass handles as an argument to a callback that you create yourself like that. As you have seen, it basically sends a time-stamped version of handles from the time the callback was declared.
Instead pass the handle to the parent figure and then within the callback get the handles from that - i.e.
handles.tgroup = uitabgroup('Parent', handles.figure1,'TabLocation', 'top','SelectionChangedFcn',...
{@tgroup_SelectionChangedFcn,handles.figure1});%This cell tells the uitabgroup
then in your callback, begin with:
function tgroup_SelectionChangedFcn(hObject, eventdata, hGUI)
handles = guidata( hGUI );
...
You do have to be a little bit careful with this if you have listeners and callbacks triggering other callbacks which are also writing things to handles, but I'll not go into details on that unless you actually encounter it as a problem.
  2 comentarios
test run
test run el 19 de Sept. de 2016
Thank you very much Adam for your answer , I have not tested it yet because by using
handles.tgroup = uitabgroup('Parent', handles.figure1,'TabLocation', 'top','SelectionChangedFcn',...
@(hObject,eventdata)Frogsim('tgroup_SelectionChangedFcn',hObject,eventdata,guidata(hObject)));
then the handles structure is updated correctly by the guidata(hObject,handles) function and integrates correctly with the other callbacks created by guide.
Although I'll give a try to your answer, there shouldn't be any problem with it because the updated structure of handles is retrieved correctly the way you do it and also you update later by using guidata(hObject,handles) which takes the handles structure "outside" to the rest of the gui.
test run
test run el 19 de Sept. de 2016
Editada: test run el 19 de Sept. de 2016
Hi again Adam, I have tested your answer it works perfectly also.
Thank you very much

Iniciar sesión para comentar.

Categorías

Más información sobre Specifying Target for Graphics Output en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by