MATLAB课程:代码示例之Graphics and Visualization(五)
Programmatically Create a User Interface with Tabbed Panels
This example shows how to create a user interface with tab panels in MATLAB®.
Objects Used to Create Tab PanelsThe tabgroup and tab objects are used to build user interfaces with tab panels. A tabgroup object is created using the uitabgroup function. A tab object is created using the uitabfunction with a tabgroup as its parent. The tab objects appear in the order in which they are created.
f = figure;tgroup = uitabgroup('Parent', f);tab1 = uitab('Parent', tgroup, 'Title', 'Loan Data');tab2 = uitab('Parent', tgroup, 'Title', 'Amortization Table');tab3 = uitab('Parent', tgroup, 'Title', 'Principal/Interest Plot');

Like all graphics objects, tabgroups and tabs have properties that you can view and modify. These properties have default values. The display of a tabgroup or a tab shows its most commonly used properties.
tgroup
tgroup = TabGroup with properties: SelectedTab: [1x1 Tab] SelectionChangedFcn: '' TabLocation: 'top' Position: [0 0 1 1] Units: 'normalized' Use GET to show all properties
tab1
tab1 = Tab (Loan Data) with properties: Title: 'Loan Data' BackgroundColor: [0.9400 0.9400 0.9400] Position: [0.0036 0.0071 0.9911 0.9357] Units: 'normalized' Use GET to show all properties
Get Individual Tabgroup and Tab PropertiesTo access individual properties, use the dot notation syntax object.Property. For example, return the TabLocation property of the tabgroup.
tgroup.TabLocation
ans =top
Change Tabgroup and Tab PropertiesTo customize the look of a tabgroup or tab object, change any property value using the dot notation syntax object.Property.
tab3.ForegroundColor = 'blue';tgroup.SelectedTab = tab1;

Add Components to TabTabs contain user interface components such as labels, buttons, and edit boxes. To add a component to a tab, create the component and set its parent to the tab.
lblLoanAmount = uicontrol('Parent', tab1, 'Style', 'text', 'String', 'Loan Amount', ... 'HorizontalAlignment', 'left', 'Position', [80 320 170 25]) ;edtLoanAmount = uicontrol('Parent', tab1, 'Style', 'edit', ... 'Position', [224 320 200 30]) ;

Create a CallbackThe tabgroup object has one callback called SelectionChangedFcn. This callback function is called when the user changes the currently selected tab.
tgroup.SelectionChangedFcn = @tabChangedCB;
For example, if you want the application to recalculate the amortization table and update the plot when the user leaves the "Loan Data" tab, then set the SelectionChangeCallback to a function with this format:
function tabChangedCB(src, eventdata)
% Get the Title of the previous tabtabName = eventdata.OldValue.Title;
% If 'Loan Data' was the previous tab, update the table and plotif strcmp(tabName, 'Loan Data') % <insert code here to update the amortization table and plot>end
end
Get All Tabgroup and Tab PropertiesGraphics objects in MATLAB have many properties. To see all the properties of a tabgroup or tab object, use the get command.
get(tgroup)
BeingDeleted: 'off' BusyAction: 'queue' ButtonDownFcn: '' Children: [3x1 Tab] CreateFcn: '' DeleteFcn: '' HandleVisibility: 'on' Interruptible: 'on' Parent: [1x1 Figure] Position: [0 0 1 1] SelectedTab: [1x1 Tab] SelectionChangedFcn: @tabChangedCB SizeChangedFcn: '' TabLocation: 'top' Tag: '' Type: 'uitabgroup' UIContextMenu: [0x0 GraphicsPlaceholder] Units: 'normalized' UserData: [] Visible: 'on'
get(tab3)
BackgroundColor: [0.9400 0.9400 0.9400] BeingDeleted: 'off' BusyAction: 'queue' ButtonDownFcn: '' Children: [0x0 GraphicsPlaceholder] CreateFcn: '' DeleteFcn: '' ForegroundColor: [0 0 1] HandleVisibility: 'on' Interruptible: 'on' Parent: [1x1 TabGroup] Position: [0.0036 0.0071 0.9911 0.9357] SizeChangedFcn: '' Tag: '' Title: 'Principal/Interest Plot' TooltipString: '' Type: 'uitab' UIContextMenu: [0x0 GraphicsPlaceholder] Units: 'normalized' UserData: []