Main Content

addStyle

Add style to table UI component

Description

example

addStyle(uit,s) adds a style created with the uistyle function to the specified table UI component. The style is applied to the whole table. The table must be parented to a figure created with the uifigure function, or to one of its child containers.

example

addStyle(uit,s,target,targetIndex) adds the style to a specific row, column, or cell. For example, addStyle(uit,s,'column',3) adds the style to the third column of the specified table.

Examples

collapse all

Create a table UI component in a figure.

fig = uifigure;
fig.Position = [500 500 520 200];

uit = uitable(fig);
uit.Data = rand(5);
uit.Position = [20 30 480 135];

Then, create a red italic font style using the uistyle function. Add it to the whole table using the addStyle function.

s = uistyle('FontAngle','italic','FontColor','r');
addStyle(uit,s)

Style cells in a table UI component that contain missing values. In this case, add a yellow background color style to cells that have NaN values.

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 the ismissing function to get a logical array of the table elements that contain missing values. Find the row and column subscripts for the elements that have NaN values. Finally, create a yellow background color style and add it to the cells with NaN values in the table UI component.

styleIndices = ismissing(tdata);
[row,col] = find(styleIndices);

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

Create multiple styles and add them to different parts of a table UI component.

Create a figure with a table UI component in it and display numeric data in the table. Find the row and column subscripts for elements in the table with a value less than zero so you can style these cells later.

fig = uifigure;
fig.Position = [500 500 720 230];

uit = uitable(fig);
uit.Data = randi([-20,20],7);
uit.Position = [20 30 680 185];

[row,col] = find(uit.Data < 0);

Create two background color styles and one style that specifies font color and weight. Add a cyan background color to columns 1, 3, and 5. Emphasize the cells with negative values by making their font red and bold. Then, style rows 3 and 4 with a green background color. Finally, reuse the cyan background color style and add it to column 7. For cells where multiple styles of the same type are added, the style that is added last is the one that displays in the cell.

s1 = uistyle;
s1.BackgroundColor = 'cyan';
addStyle(uit,s1,'column',[1 3 5])

s2 = uistyle;
s2.FontColor = 'red';
s2.FontWeight = 'bold';
addStyle(uit,s2,'cell',[row,col])

s3 = uistyle;
s3.BackgroundColor = 'green';
addStyle(uit,s3,'row',[3 4])

addStyle(uit,s1,'column',7)

Input Arguments

collapse all

Table component, specified as a Table object created with the uitable function. The Table object must be parented to a figure created with the uifigure function, or one of its child containers.

Style object created with the uistyle function.

Style target, specified as 'row', 'column', 'cell', or 'table'. Use this argument to indicate the type of table part to apply the style to.

Example: addStyle(uit,s,'column',5) adds the style to the fifth column of uit.

Example: addStyle(uit,s,'cell',[3 7]) adds the style to the cell in row 3, column 7.

Style target index, specified as one of the values listed in the tables. The types of values that you can specify depend on the target and the type of data used in the table UI component.

TargetSupported ValuesExamplesResult
'row'

Positive integer.

4

Adds a style to the row with the corresponding row index.

Vector of positive integers

[3 8 9 12]

Adds a style to the rows with the corresponding row indices.

'column'

Positive integer.

3

Adds a style to the column with the corresponding column index.

Vector of positive integers.

[1 2 7 14]

Adds a style to the columns with the corresponding column indices.

'cell'

n-by-2 array of positive integers.

[2 4;5 9;13 27]

Adds a style to the cells with the corresponding row and column subscripts.

'table'

Empty character vector.

''

Adds a style to the entire table.

In table UI components in which the underlying data is a table array, there are additional options when the target is specified as 'column'. Specify these values for the targetIndex if you want to apply a style to a column based on the variable names in the table array.

TargetSupported ValuesExamples
'column'

String scalar.

Adds a style to the column with the corresponding variable name.

"Torque"

String array.

Adds a style to the columns with the corresponding variable names.

["Torque" "Mass"]

Character vector.

Adds a style to the column with the corresponding variable name.

'Revenue'

1-D cell array of character vectors

Adds a style to the columns with the corresponding variable names.

{'Year','Expenses','Revenue'}

If you are styling cells based on whether the value of the cells meets a specific condition, and your cells are editable, then use a CellEditCallback function to recompute the style target indices that meet the specified condition, and add a new style to the table that sets these new cells as the targetIndex.

Tips

  • To see a list of the styles that have been added to a table, query the value of the StyleConfigurations property.

Introduced in R2019b