全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 数据可视化
1003 1
2016-03-08

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 Panels

The 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 Properties

To 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 Properties

To 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 Tab

Tabs 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 Callback

The 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 Properties

Graphics 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: []



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2016-3-8 15:09:00
好样的,受教了。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群