Editable Table in MATLAB

A table that can be added to a figure; with editing and scrolling.
Updated 31 Aug 2006

View License

This provides a table as a separate MATLAB script which can be called from any figure. The table can reside within a figure with other UI controls. The fields can be edited and the table can be scrolled. Numeric or string fields are supported. More than one table per figure should be possible.

You can create a table as follows (just running the script with no arguments does this):

fig = nf;
tbl = axes('units', 'pixels','position', [10 10 400 100]);
cell_data = {...
'Alpha', 1, 2, 3,'';...
'Bravo', 4, 5, 6,'';...
'Charlie', 7, 8, 9,'';...
'Dog', 10,11,12,'';...
'Echo', 13,14,15,'';...
'Golf', 19,20,21,'';...
'Hotel', 26,27,28,'';...

columninfo.titles={'Param','Lower Limit','Upper Limit',...
'Initial Value','Result'};
columninfo.formats = {'%4.6g','%4.6g','%4.6g','%4.6g','%4.6g'};
columninfo.weight = [ 1, 1, 1, 1, 1];
columninfo.multipliers = [ 1, 1, 1, 1, 1];
columninfo.isEditable = [ 1, 1, 1, 1, 0];
columninfo.isNumeric = [ 0, 1, 1, 1, 1];
columninfo.withCheck = true;
columninfo.chkLabel = 'Use';
rowHeight = 16;

mltable(fig, tbl, 'CreateTable', columninfo, rowHeight, cell_data,...

When you click on a cell which is "Editable", the cell changes color. When you click again, the editing cursor appears and you can edit the cell contents. To finish editing, press then Enter key or click on another cell.

If you use the table in a GUI form designed with GUIDE, you probably want to end the editing when you click on another part of the form. To do this, add the following lines after the one which creates the table:

endfcn = sprintf('mltable(%14.13f, %14.13f, ''SetCellValue'');',...

To get the current table contents, use:

info = get(tbl,'UserData');
data = info.data;

The variable "data" now contains a cell array of the table contents.

This describes the "columninfo" structure:
Titles: array of strings to place above each column. Use '' for non-labeled columns.
Weight: array of column widths. Use 1 for unit width, > 1 for wider columns. This does not work for column 1 (bug!)
Multipliers: Use to scale numeric values. For instance, if Multiplier for column 2 is set to .01, then the value displayed in column 2 will always be 1/100 of the value in the data array.
Formats: sprintf style formats for each column. Ignored for non-numerics. Use '%f' if in doubt.
IsEditable: array of 1s and 0s for each column. 0 means column is read-only.
isNumeric: array or 1s and 0s for each column. 1 means treat column as numeric (will do scaling by Multiplier, check for non-numeric input, and will store the value as a number, not a string.)
withCheck: optional field; if set to "true", the table will have a column of checkboxes along the left side.
chkLabel: optional field: if set to a string, this will be placed above the column of checkboxes if there is one.
rowsFixed: the table won't have buttons for insert/delete rows, even if some fields are editable

How to use check boxes:
If use set a field named 'withCheck' in columnInfo to 'true', then the table will have a column of checkboxes along the left hand side. You can set the initial state of the check boxes with code like this:

for ii = 1:numrows
mltable(fig, tbl, 'SetCheck',[],[],[],[],ii, checkval(ii));

assuming that checkval is a vector of 1s and 0s to determine the initial state.
After the figure is created, you can get the state of the checkboxes with code like this:

data = get(tbl, 'UserData');
checkval = data.isChecked;

and now checkval is a vector of 1s and 0s depending on the checkbox states.

How to use DoubleClick

You can have the table call your own routine when the user double clicks on a cell.

mltable(fig, hobj, 'SetDblClick', 'Myfunc', 'fmt_str');

where 'Myfunc' is a string naming the function to call, and 'fmt_str' is a string representing additionial arguments. If you are using the table in a Guide figure, use SetDblClick like this:

mltable(gcf, handles.tbl, 'SetDblClick', [], [], [], [], 'myfig', 'dblClick_Callback');

where 'myfig' is the name of the main figure and dblClick_Callback is a function within that figure's main file.

Your function will be called as

[] = Myfunc(hObject, [], handles, arg1, arg2, ...)

where arg1, arg2, etc are the argument names you passed to SetDblClick in the format string.

(Based on code by a colleague, Greg Gershanok, with his permission.)

Cite As

Morris Maynard (2024). Editable Table in MATLAB (https://www.mathworks.com/matlabcentral/fileexchange/6734-editable-table-in-matlab), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R14SP2
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Migrate GUIDE Apps in Help Center and MATLAB Answers

Inspired: uitable

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

Changes from 1-30-06 didn't take:

Add some actions: UpdateData, OnCheck, Version
Better row sizing behavior
Works with a panel as parent
Make formats optional in column info
Sets 'dirty' field automatically if there is no OnSetCell callback.