Programmatic App that Displays a Table - MATLAB & Simulink
Documentation All Examples Functions
Search R2019a Documentation
CONTENTS
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
[Link] ق۰۸:۲۵:۴۴ ۰۵/۱۸/۲۰۱۹]
Programmatic App that Displays a Table - MATLAB & Simulink
PatientData = [LastName num2cell([Age Weight Height]) SelfAssessedHealthStatus]; % convert to cell
array
[Link] = 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.
[Link] = {'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.
[Link] = {100, 'auto', 'auto', 'auto', 150};
[Link] ق۰۸:۲۵:۴۴ ۰۵/۱۸/۲۰۱۹]
Programmatic App that Displays a Table - MATLAB & Simulink
To remove the row names, set the RowName property to an empty array ([]).
[Link] = [];
Resize the table and remove any extra space using the Position property.
[Link] = [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.
[Link] ق۰۸:۲۵:۴۴ ۰۵/۱۸/۲۰۱۹]
Programmatic App that Displays a Table - MATLAB & Simulink
[Link] = [.4 .4 .4; .4 .4 .8];
[Link] = [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.
[Link] = [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.
[Link] = {[] [] [] [] {'Excellent', 'Fair', 'Good', 'Poor'}};
[Link] ق۰۸:۲۵:۴۴ ۰۵/۱۸/۲۰۱۹]
Programmatic App that Displays a Table - MATLAB & Simulink
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.
[Link] = @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 ([Link](2) == 2 && ... % check if column 2
([Link] < 0 || [Link] > 120))
tableData = [Link];
tableData{[Link](1), [Link](2)} = [Link];
[Link] = tableData; % set the data back to its original value
warning('Age must be between 0 and 120.') % warn the user
end
end
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.
get(t)
BackgroundColor: [2x3 double]
BeingDeleted: 'off'
BusyAction: 'queue'
ButtonDownFcn: ''
CellEditCallback: @ageCheckCB
CellSelectionCallback: ''
Children: [0x0 handle]
ColumnEditable: [0 1 1 1 1]
ColumnFormat: {[] [] [] [] {1x4 cell}}
[Link] ق۰۸:۲۵:۴۴ ۰۵/۱۸/۲۰۱۹]
Programmatic App that Displays a Table - MATLAB & Simulink
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 [Link]]
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'
Trial Software
Product Updates
[Link] ق۰۸:۲۵:۴۴ ۰۵/۱۸/۲۰۱۹]
Programmatic App that Displays a Table - MATLAB & Simulink
MATLAB Documentation Support
Examples MATLAB Answers
Functions Installation Help
Release Notes Bug Reports
PDF Documentation Product Requirements
Software Downloads
Introducing Deep Learning with
MATLAB
Download ebook
Explore Try or Buy Learn to Use Get Support About
Products MathWorks
Downloads Documentation Installation Help
MATLAB Careers
Trial Software Tutorials Answers
Simulink Newsroom
Contact Sales Examples Consulting
Student Software Social Mission
Pricing and Videos and License Center
Hardware Licensing Webinars Contact Us
Support
How to Buy Training About
File Exchange MathWorks
MathWorks
Accelerating the pace of engineering and science
MathWorks is the leading developer of mathematical computing software for engineers and scientists.
Discover...
United States
Patents Trademarks Privacy Policy Preventing Piracy Application Status
© 1994-2019 The MathWorks, Inc.
Join the conversation
[Link] ق۰۸:۲۵:۴۴ ۰۵/۱۸/۲۰۱۹]