Main Content

uitable

Create table user interface component

Description

uit= uitablecreates a table user interface component in the current figure and returns theTableUI component object. If there is no figure available, MATLAB®calls thefigurefunction to create one.

uit= uitable(Name,Value)specifies property values of the table UI component using one or more name-value pair arguments.

example

uit= uitable()creates the table in the specified parent container. The parent container can be a figure created with either thefigureoruifigurefunction, or a child container such as a panel. Property values foruitablevary slightly depending on whether the app is created with thefigureoruifigurefunction. For more information, seeName-Value Pair Arguments.

example

uit= uitable(,Name,Value)specifies the parent container and one or more property values.

Examples

collapse all

Starting in R2018a, you can displaytablearray data in a table UI component. This type of data is supported only when the table UI component is in a figure created with theuifigurefunction. App Designer uses this type of figure for creating apps.

Create table arraytby calling thereadtablefunction to read data from a file. Select four variables and 15 rows fromt.

t = readtable('patients.xls'); vars = {'Age','Systolic','Diastolic','Smoker'}; t = t(1:15,vars);

Create a table UI component, and specifytas the data.

fig = uifigure; uit = uitable(fig,'Data',t);

Starting in R2019a, you can sort the columns of a table UI component that hastablearray data stored in itsDataproperty. This type of data is supported only when the table UI component is in a figure created with theuifigurefunction. App Designer uses this type of figure for creating apps.

Displaytablearray data and update the plot when a user sorts the columns or edits the cells.

First, create a program file calledtsunamisData.m. Within the program file:

  • Create atablearray by calling thereadtablefunction.

  • Create a sortable and editable table UI component to display in a figure. Store thetablearray to component'sDataproperty.

  • Specify aDisplayDataChangedFcncallback that uses theDisplayDataproperty to update the plot when a user sorts columns or edits cells in the table UI component.

functiontsunamisData t = readtable('tsunamis.xlsx'); vars = {'Year','MaxHeight','Latitude','Longitude'}; t = t(1:20,vars); fig = uifigure; fig.Position(3:4) = [822 360]; uit = uitable(fig); uit.Data = t; uit.ColumnSortable = [false true true true]; uit.ColumnEditable = true; uit.Position(3) = 375; uit.DisplayDataChangedFcn = @updatePlot; ax = uiaxes(fig); ax.Position(1) = 415; ax.YLabel.String ='Max Height'; x = t.Year; y = t.MaxHeight; area(ax,x,y)functionupdatePlot(src,event) t = uit.DisplayData; x = t.Year; y = t.MaxHeight; area(ax,x,y)endend

A sortable column displays arrows in the header when you hover your mouse over it. Find a sortable column and sort the table. Notice how the displayed data and the plot update after sorting.

Starting in R2019b, you can style rows, columns, or cells of a table UI component using theuistyleandaddStylefunctions. Styles are only supported when the table UI component is in a figure created with theuifigurefunction. App Designer uses this type of figure for creating apps.

风格细胞选项卡le UI component that contain missing values. In this case, add a yellow background color style to cells that haveNaNvalues.

Read tsunami sample data into the workspace as a table array. Then, create a table UI component to display the data.

tdata = readtable('tsunamis.xlsx'); vars = {'Year','Month','Day','Hour',...'MaxHeight','Cause','EarthquakeMagnitude'}; tdata = tdata(1:100,vars); fig = uifigure('Position',[500 500 750 350]); uit = uitable(fig); uit.Position = [20 20 710 310]; uit.Data = tdata; uit.RowName ='numbered';

Use theismissingfunction to get a logical array of the table elements that contain missing values. Find the row and column subscripts for the elements that haveNaNvalues. Finally, create a yellow background color style and add it to the cells withNaNvalues in the table UI component.

styleIndices = ismissing(tdata); [row,col] = find(styleIndices); s = uistyle('BackgroundColor','yellow'); addStyle(uit,s,'cell',[row,col]);

Create a table UI component that displays a 10-by-3 array of random integers. TheDataproperty specifies the values to display, and thePositionproperty specifies the location and size of the table within the figure.

f = figure; uit = uitable(f,'Data',randi(100,10,3),'Position',[20 20 262 204]);

Table UI components can accommodate a mixture of different data types across the columns.

Create an emptyTableUI component.

f = figure; uit = uitable(f);

Set theDataproperty to populate the data as a cell array that contains a mixture of different types. Then set thePositionproperty to adjust the location and size of the table to fit the data.

d = {“男”,52,true;“男”,40,true;'Female',25,false}; uit.Data = d; uit.Position = [20 20 258 78];

Set theColumnNameproperty to change the column headings to descriptive names. Set theColumnEditableproperty totrueso that users can edit the data in the UI. When a user changes a value in the UI, theDataproperty updates to reflect that change.

uit.ColumnName = {'Gender','Age','Authorized'}; uit.ColumnEditable = true;

Input Arguments

collapse all

Parent container, specified as a figure created with either thefigureoruifigurefunction, or a child container:

  • Panels, tabs and button groups can be containers in either type of figure.

  • Grid layouts can be containers only in figures created with theuifigurefunction.

Name-Value Pair Arguments

Example:'Data',[1 2 3; 4 5 6]

Specify optional comma-separated pairs ofName,Valuearguments.Nameis the argument name andValueis the corresponding value.Namemust appear inside single quotes (' '). You can specify several name and value pair arguments asName1,Value1,...,NameN,ValueN.

You can setTableproperties using Name-Value pair arguments.

  • For a list of properties available for apps created with theuifigurefunction or in App Designer, seeTable Properties.

  • For a list of properties available for apps created with thefigurefunction, seeTable Properties.

Introduced in R2008a