File Exchange

## DataTable

version 1.1 (11.9 KB) by

store data in a table format that supports printing in plain text, Latex, HTML, Wiki,...

Updated

Editor's Note: This file was selected as MATLAB Central Pick of the Week

DataTable objects are used to store data in a table format that is easily expandable, manipulatable and that supports printing in a variety of formats (plain text, Latex, HTML, Wiki; new printing formats can be added without changing any code). The format of numeric table contents (e.g. number of displayed digits) and the alignment of text and numbers can be changed for each column individually.

To use, extract zip-archive and add the extracted folder ('datatablepackage') to your matlab path.

Type 'help DataTable' for more information and examples on how to use DataTable.

The new object-oriented programming in MATLAB 7.6 (R2008a) or higher is required for DataTable to work.

jose herrero

### jose herrero (view profile)

thanks so much paul. i'm trying to write the table to a variable as follows:
fileID = fopen('testV.mat')
table.toText(fileID)
but the testV=[]
what should I change?

Yuri K

### Yuri K (view profile)

Your solution leaves spaces on both sides of the delimiter. So it's actually
0.8147<space><tab><space>0.9134 etc.

Paul Mattern

### Paul Mattern (view profile)

>> dt = DataTable();
>> dt{1:3,1:3} = rand(3);
>> dt.toText('delimiter', char(9)) % char(9) is a tab

0.8147 0.9134 0.2785
0.9058 0.6324 0.5469
0.1270 0.0975 0.9575

Or are you looking for something else?

Yuri K

### Yuri K (view profile)

Paul, thanks for the great submission!

I don't think you need lines 448-451 since you process empty table inside the printer anyway.

I made a very simple TextFilePrinter to output the table to standard tab delimited text file. Hope you'd consider to include such functionality into the package.

Paul Mattern

### Paul Mattern (view profile)

Hi Nicholaus,

the short answer is no, right now the TablePrinters get no information about the line number they are printing. So, it would require quite a bit of recoding to make it work. You could however use a (rather ugly) hack to get that extra \hline:

dt = DataTable();

... % fill table here

tmp = dt.getData(1,end);
dt{1,end} = sprintf('%g \\\\ \\hline %%', tmp{1});

dt.toLatex()

This might be enough for some quick printing of results, but of course it destroys the nice look of the table if you print it in any other format than Latex.

If some more people request a feature like this I may add it when I find some time.

Nicholaus Halecky

### Nicholaus Halecky (view profile)

Hey Paul,
Thanks much for this great function -- I have just implemented it for printing some results and it is working quite well. Quick question, I use latex often and was wondering if it is easy to make basic changes to the Latex TablePrinter class/method -- specifically, I wanted to have the option to an \hline after the first row, thus distinguishing row titles from the datasets. Any thoughts?
I really appreciate it.
Thanks again.

Torfinn

### Torfinn (view profile)

I was just looking to do this myself, luckily you saved me some time! One thing I had to modify for my use though: I wanted to print the table as a comment, thus I had to insert a '%' as a prefix for every line. I think an extra parameter to give prefixes would be useful for others as well.

Matthew

Paul Mattern

### Paul Mattern (view profile)

Thanks for the comment. I just submitted the new version and it should be available shortly.

For a quick fix replace line 933 in DataTable.m:
strdata = myformat(this.applyColumnFormat());
by
strdata = this.standardFormat(this.applyColumnFormat());

More infos about the bug: The function "myformat" only exists on my computer and has been replaced by standardFormat in the DataTable class. Unfortunately I did not replace it everywhere.

Kevin

### Kevin (view profile)

I get this error when I try to run the code with the following.

>> table=DataTable();
>> table{1:10,1:10}=3

10x10 DataTable
??? Undefined function or method 'myformat' for input arguments of type 'cell'.

Error in ==> DataTable>DataTable.printTableBody at 933
strdata = myformat(this.applyColumnFormat());

Error in ==> DataTable>DataTable.toText at 211
this.printTableBody(fid, tight, TextTablePrinter(delimiter))

Error in ==> DataTable>DataTable.display at 921
this.toText();