Code covered by the BSD License  

Highlights from
Editable Table in MATLAB

4.55172

4.6 | 29 ratings Rate this file 63 Downloads (last 30 days) File Size: 8.55 KB File ID: #6734
image thumbnail

Editable Table in MATLAB

by

 

17 Jan 2005 (Updated )

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

| Watch this File

File Information
Description

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

Acknowledgements

This file inspired Uitable.

MATLAB release MATLAB 7.0.4 (R14SP2)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (29)
19 Aug 2011 Pieter van den Berg  
21 May 2011 Musa

perfect and really useful..

12 Jan 2011 Barry

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

01 Oct 2009 katu

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.

10 Jul 2009 Arthur  
27 Nov 2008 Erin Macabee  
02 Oct 2008 Jian Fu

Cool! u did a big favor to me !

22 Sep 2008 Cristina Ani  
18 Aug 2008 Ori Negri

thanks, it helped me a lot

26 Jun 2008 hma abaza

it is very good

02 Jun 2008 emnon diye

good

14 May 2008 u p  
30 Jan 2008 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.

11 Jan 2008 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

03 Nov 2007 saber neamati  
08 Oct 2007 Adolfo Cardona

Thanks

13 Jun 2007 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!

11 May 2007 V Shestakov

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

02 Jan 2007 Michal Perl

Very nice look, and easy to use

26 Dec 2006 Felipe Gonzalez

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

18 Jan 2006 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.

14 Nov 2005 Dan Johnson

This is BETTER than sliced bread

09 Nov 2005 Tolga Örter

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

22 Aug 2005 Yakov Elashvili  
21 Aug 2005 Rafy Azman

I luv this very much!!

17 Aug 2005 Elmar Tarajan

Great job!

02 Jun 2005 Günter Puhr

very usefull

10 Feb 2005 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

22 Jan 2005 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
20 Jan 2005

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

20 Jan 2005

Bug fixes;file rename

20 Jan 2005

Updated description and file.

21 Jan 2005

Added row-select highlighting feature
Updated description

24 Jan 2005

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

24 Jan 2005

Updated description

25 Jan 2005

Include 'trim' function which is missing on some systems

25 Jan 2005

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

28 Jan 2005

Add optional checkboxes on left side

11 Feb 2005

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

14 Feb 2005

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

17 Feb 2005

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

25 Apr 2005

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

31 Jan 2006

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

Contact us