File Exchange

image thumbnail

Editable Table in MATLAB

version 1.0 (8.55 KB) by

A table that can be added to a figure; with editing and scrolling.

4.55172
29 Ratings

11 Downloads

Updated

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,'';...
'Foxtrot',16,17,18,'';...
'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;
gFont.size=9;
gFont.name='Helvetica';

mltable(fig, tbl, 'CreateTable', columninfo, rowHeight, cell_data,...
        gFont);

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'');',...
                  hObject,handles.tblParams);
set(hObject,'buttondownfcn',endfcn);

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));
end

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.)

Comments and Ratings (33)

Sanjeev Goyal

Error using matlab.ui.Figure/set
Invalid or deleted object.

Error in mltable>createTable (line 149)
set(fig, 'CurrentAxes', data.axes);

Error in mltable (line 77)
    data = createTable(fig, hObj, columnInfo, rowHeight, cell_data, gFont);

When I am trying to run your script. I am getting the above error. Please help. Thanks

how could i put this table in a GUI interface ?

Daniel

Daniel (view profile)

Mahfoud: I had exactly the same error and I changed the fig and hObj inputs to fig.Number and hObj.Position and it worked for me. I hope it works for you as well.

calling your function in Matlab R2014b seems to rise an error as follow :

Error using sprintf
Function is not defined for 'matlab.ui.Figure' inputs.

Error in mltable>createTable (line 162)
editfcn = sprintf('mltable(%14.13f, %14.13f, ''EditCell'');',fig, hObj);

any suggestion would be apreciated thanx...

Musa

Musa (view profile)

perfect and really useful..

Barry

Barry (view profile)

Amazing... very helpful. How could i make data in the center instead of left-justify.

katu

katu (view profile)

Great work, but when you use it in a GUI, It seems to get smaller every time you refresh the table.
To avoid this I deleted lines 154-159 and it works.

Arthur

Erin Macabee

Jian Fu

Cool! u did a big favor to me !

Cristina Ani

Ori Negri

thanks, it helped me a lot

hma abaza

it is very good

emnon diye

good

u p

Kiran S

Very useful, especially to modify the column and row heights and to create nice GUI look, when compared to flexgrid activex control.

Only problem is we cannot use popup menu control when mltable is placed on figure window. popup menu is becoming invisible. Any solution for this is welcome.

Setu Havanur

Hi,

I tried just copying and running it.....it says 'undefined function or variable'. Could you help me? I actually need to create tables.

Thanks

saber neamati

Adolfo Cardona

Thanks

emilie hustaix

Merci merci merci merci !! d'une etudiante en stage au Canada qui a decouvert Matlab il y a un mois et a qui on demande d'afficher un tableau dans un GUI !!! Vous me sauvez la vie!! Merci encore, tout marche tres bien!

V Shestakov

Much thanks!!! It help me. I could do my work very quickly.

Michal Perl

Very nice look, and easy to use

Felipe Gonzalez

EXCELLENT. This tipe of files are rarely found here. amazing work. I've learned so much reading from it.

Peter Krug

It is very versatile, but some people do not need this, just to put a table into a figure without any fancy stuff.

Dan Johnson

This is BETTER than sliced bread

Tolga Örter

Very usefull tool!!!
But how can i draw mor than one table in a GUI created by GUIDE.

Yakov Elashvili

Rafy Azman

I luv this very much!!

Elmar Tarajan

Great job!

Günter Puhr

very usefull

Poul-Erik Hansen

Great work!
mltable works better than matlabs uitable.
It would have been nice to have more examples exploring the possibilities of mltable

Mirjam Blokker

small bug when height of table is larger than number of rows*row_height then it creates more rows, tries to fill them and crashes, solved this by adjusting the table height (row 76):

ss = size(data.data);
numFilledRows = ss(1);
if numFilledRows < data.numRows
    data.numRows = numFilledRows;
    pos_ax(4) = rowHeight * (1+data.numRows); % one extra for title
end

Updates

Replace rowHeight with rowInfo SetDblClick renamed to SetOnDblClick Add OnCheck action and SetOnCheck
Add 'Version' action
Column formats are now optional
Add optional 'units' field to ColInfo
Add some examples

Add callback for "OnSetCell' action (works same as OnDblClick)
Better centering for titles.
Eliminate use of 'gcf'.

Add optional field in columnInfo to prevent adding the ins/del buttons
Removed commented code now that title row is working ok
Fix bugs with using multipliers ~= 1
Fix bug when clicking on title row

Make titles look more like part of table
Updated image
Updated description

Fixed bug in resizing table with checkboxes during create and destroy actions
Demo column weight feature in mltest function

Add optional checkboxes on left side

Include "trim" function in file, since it is missing on some systems.

Include 'trim' function which is missing on some systems

Updated description

Add DestroyTable action
Add double-click action for row item
Change edit-box handling so it will be the proper size always
Handle the '_' character since if not strings are not displayed correctly

Added row-select highlighting feature
Updated description

Updated description and file.

Bug fixes;file rename

Added a help header with usage example.
Renamed file to 'mltable.m'

MATLAB Release
MATLAB 7.0.4 (R14SP2)
Acknowledgements

Inspired: uitable

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video