Programmatic App that Displays a Table

This example shows how to create a table in an app using the uitable function. It also shows how to modify the appearance of the table and how to restrict editing of the table in the running app.

Create a Table Containing Simple Numeric Data

The uitable function creates an empty table. You can populate the table by setting the Data property. For example, you can create a table containing magic square values.

f = figure('Position', [100 100 752 250]);
t = uitable('Parent', f, 'Position', [25 50 700 200], 'Data', magic(10))

t = 
  Table with properties:

                Data: [10x10 double]
         ColumnWidth: 'auto'
      ColumnEditable: []
    CellEditCallback: ''
            Position: [25 50 700 200]
               Units: 'pixels'

  Show all properties

Create a Table Containing Mixed Data Types

Display mixed data types by setting the Data property to a cell array.

load patients LastName Age Weight Height SelfAssessedHealthStatus                   % load data
PatientData = [LastName num2cell([Age Weight Height]) SelfAssessedHealthStatus];    % convert to cell array

t.Data = PatientData;

Customize the Display

You can customize the display of a table in several ways. Use the ColumnName property to add column headings. To create multiline headings, use the pipe (|) symbol.

t.ColumnName = {'LastName', 'Age', 'Weight', 'Height', 'Self Assessed|Health Status'};

To adjust the widths of the columns, specify the ColumnWidth property. The ColumnWidth property is a 1-by-N cell array, where N is the number of columns in the table. Set a specific column width, or let MATLAB® set the width based on the contents.

t.ColumnWidth = {100, 'auto', 'auto', 'auto', 150};

To remove the row names, set the RowName property to an empty array ([]).

t.RowName = [];

Resize the table and remove any extra space using the Position property.

t.Position = [15 25 495 200];

By default, tables use row striping. To turn off row striping, set the RowStriping property to 'off'. To control the colors of the stripes, set two different colors for the BackgroundColor property. Use the ForegroundColor property to control the color of the text.

t.BackgroundColor = [.4 .4 .4; .4 .4 .8];  
t.ForegroundColor = [1 1 1];

Restrict Editing of Cell Values

To restrict the user's ability to edit data in the table, set the ColumnEditable property. By default, data cannot be edited in the app. Setting the ColumnEditable property to true for a column allows the user to edit data in that column.

t.ColumnEditable = [false true true true true];

Change Column Format

The ColumnFormat property controls how data is displayed and edited. To specify choices in a drop-down list, specify a cell array of character vectors as the column format. In this example, the Self Assessed Health Status column has a drop-down list containing four options: Excellent, Fair, Good, and Poor.

t.ColumnFormat = {[] [] [] [] {'Excellent', 'Fair', 'Good', 'Poor'}};

Create a Callback

The Table object has two commonly used callbacks. The CellSelectionCallback executes when the user selects a different cell. The CellEditCallback executes when the user changes a value in a cell.

t.CellEditCallback = @ageCheckCB;

For example, if you want the Age column to contain values between 0 and 120, set the CellEditCallback to a function such as this one:

function ageCheckCB(src, eventdata)
if (eventdata.Indices(2) == 2 && ...                  % check if column 2
      (eventdata.NewData < 0 || eventdata.NewData > 120))
   tableData = src.Data;
   tableData{eventdata.Indices(1), eventdata.Indices(2)} = eventdata.PreviousData;
   src.Data = tableData;                              % set the data back to its original value
   warning('Age must be between 0 and 120.')          % warn the user

If the user enters a value that is outside the acceptable range, the callback function returns a warning and sets the cell value back to the previous value.

Get All Table Properties

To see all the properties of the table, use the get command.

          BackgroundColor: [2x3 double]
             BeingDeleted: 'off'
               BusyAction: 'queue'
            ButtonDownFcn: ''
         CellEditCallback: @ageCheckCB
    CellSelectionCallback: ''
                 Children: [0x0 handle]
           ColumnEditable: [0 1 1 1 1]
             ColumnFormat: {[]  []  []  []  {1x4 cell}}
               ColumnName: {5x1 cell}
           ColumnSortable: []
              ColumnWidth: {[100]  'auto'  'auto'  'auto'  [150]}
                CreateFcn: ''
                     Data: {100x5 cell}
                DeleteFcn: ''
              DisplayData: {100x5 cell}
    DisplayDataChangedFcn: ''
                   Enable: 'on'
                   Extent: [0 0 479 1940]
                FontAngle: 'normal'
                 FontName: 'Helvetica'
                 FontSize: 10
                FontUnits: 'points'
               FontWeight: 'normal'
          ForegroundColor: [1 1 1]
         HandleVisibility: 'on'
            InnerPosition: [15 25 495 200]
            Interruptible: 'on'
              KeyPressFcn: ''
            KeyReleaseFcn: ''
                   Layout: [0x0 matlab.ui.layout.LayoutOptions]
            OuterPosition: [15 25 495 200]
                   Parent: [1x1 Figure]
                 Position: [15 25 495 200]
     RearrangeableColumns: 'off'
                  RowName: ''
              RowStriping: 'on'
                      Tag: ''
                  Tooltip: ''
                     Type: 'uitable'
            UIContextMenu: [0x0 GraphicsPlaceholder]
                    Units: 'pixels'
                 UserData: []
                  Visible: 'on'