KerMor  0.9
Model order reduction for nonlinear dynamical systems and nonlinear approximation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PrintTable Class Reference

PrintTable: Class that allows table-like output spaced by tabs for multiple rows. More...

Detailed Description

PrintTable: Class that allows table-like output spaced by tabs for multiple rows.

Allows to create a table-like output for multiple values and columns. A spacer can be used to distinguish different columns (PrintTable.ColSep) and a flag (PrintTable.HasHeader) can be set to insert a line of dashes after the first row. The table can be printed directly to the console or be written to a file.

LaTeX support
Additionally, LaTeX is supported via the property PrintTable.Format. The default value is plain, which means simple output as formatted string. If set to tex, the table is printed in a LaTeX table environment (PrintTable.ColSep is ignored and & used automatically).
Cell contents
The cell content values can be anything that is straightforwardly parseable. You can pass char array arguments directly or numeric values; even function handles and classes (handle subclasses) can be passed and will automatically be converted to a string representation.
Custom cell content formatting
However, if you need to have a customized string representation, you can specifiy a cell array of strings as the last argument, containing custom formats to apply for each passed argument. Two conditions apply for this case:
  • If the cell contains more than one value, there must be one argument for each column of the PrintTable -The column contents must be valid arguments for sprintf if containing a string, or hold a function handle taking one argument and returning a char array.

When passing a function handle, it may also take two arguments. The second argument is then used to pass in the column number which is currently formatted.

Transposing
An overload for the ctranspose-method of MatLab is available which easily switches rows with columns.
Examples
% Simply run
% or
PrintTable.test_PrintTable_RowHeader_Caption;
% or
% Or copy & paste
t.addRow('123','456','789');
t.addRow('1234567','1234567','789');
t.addRow('1234567','12345678','789');
t.addRow('12345678','123','789');
% sprintf-format compatible strings can also be passed as last argument:
% single format argument:
t.addRow(123.456789,pi,789,{'%3.4f'});
% custom format for each element:
t.addRow(123.456789,pi,789,{'%3.4f','%g','format. dec.:%d'});
t.addRow('123456789','12345678910','789');
t.addRow('adgag',uint8(4),4.6);
t.addRow(@(mu)mu*4+56*245647869,t,'adgag');
t.addRow('adgag',4,4.6);
% Call display
t.display;
t.HasHeader = true;
% or simply state the variable to print
t
% Transpose the table:
tt = t';
tt.Caption = 'This is me, but transposed!';
tt.print;
% To use a different column separator just set e.g.
t.ColSep = ' -@- ';
t.display;
% PrintTable with "row header" mode
t = PrintTable('This is PrintTable-RowHeader-Caption test, created on %s',datestr(now));
t.HasRowHeader = true;
t.HasHeader = true;
t.addRow('A','B','C');
t.addRow('header-autofmt',456,789,{'%d'});
t.addRow(1234.345,456,789,{'%2.2E','%d','%d'});
t.addRow('header-expl-fmt',456,pi,{'%s','%d','%2.2f'});
t.addRow('nofmt-header',456,pi,{'%d','%f'});
t.addRow('1234567','12345','789');
t.addRow('foo','bar',datestr(clock));
t.display;
% Latex output
t.Format = 'tex';
t.Caption = 'My PrintTable in LaTeX!';
t.print;
% Printing the table:
% You can also print the table to a file. Any MatLab file handle can be used (any first
% argument for fprintf). Run the above example, then type
fid = fopen('mytable.txt','w');
% [..do some printing here ..]
t.print(fid);
% [..do some printing here ..]
fclose(fid);
% Saving the table to a file:
% Plaintext
t.saveToFile('mytable_plain.txt');
% LaTeX
t.saveToFile('mytable_tex.tex');
% Tight PDF
t.saveToFile('mytable_tightpdf.pdf');
% Whole page PDF
t.TightPDF = false;
t.saveToFile('mytable_tightpdf.pdf');
Note
Of course, any editor might have its own setting regarding tab spacing. As the default in MatLab and e.g. KWrite is four characters, this is what is used here. Change the TabCharLen constant to fit to your platform/editor/requirements.
See Also
fprintf sprintf
Author
Daniel Wirtz
Date
2011-11-17
Those links were helpful for programming the PDF export
Change in 0.7:
(Daniel Wirtz, 2013-08-10) The format cell argument can now also hold function handles for advanced formatting.
Change in 0.7:
(Daniel Wirtz, 2013-04-09) Added some table cell spacing by default ( \(\LaTeX\) arraystretch)
Change in 0.7:
(Daniel Wirtz, 2013-04-05)
  • Automatically stripping newline characters from any strings
  • Added some more verbose output about table creation to \(\LaTeX\) output
New in 0.7:
(Daniel Wirtz, 2013-02-19) Added a new method "append" to join data from a second table into the current one. Specific columns may be given.
Change in 0.7:
(Daniel Wirtz, 2013-02-19)
  • Bugfix: Accidentally also wrapped in $$ for plain text output by default.
Change in 0.7:
(Daniel Wirtz, 2013-02-15)
  • Bugfix for empty cell string
  • LaTeX alignment for first column is "r" if HasRowHeader is used
New in 0.7:
(Daniel Wirtz, 2013-02-14)
  • Added a new property "TexMathModeDetection" that automatically detects (via str2num and containment) if a cell content can be interpreted as a latex math mode value and wraps it in dollar characters when LaTeX output is set. This is switched on by default.
  • New property "StripInsertedTabChars" which causes automatic stripping of tab characters passed as arguments or created by sprintf formats or callbacks. This is switched on by default.
  • Added a short comment describing the applied settings to \(\LaTeX\) output.
  • Added a removeRow method.
  • Smaller improvements for LaTex export & added test/demo cases
Change in 0.7:
(Daniel Wirtz, 2012-12-11) Improved automatic setting of TabCharLen default values.
New in 0.6:
(Daniel Wirtz, 2012-09-19) Added printing support for function handles and improved output for numerical values
New in 0.6:
(Daniel Wirtz, 2012-07-16)
  • Added an overload for the "ctranspose" method of MatLab; now easy switching of rows and columns is possible. Keeping the HasHeader flag if set.
  • Fixed a problem with LaTeX export when specifying only a filename without "./" in the path
  • Made the TabCharLen a public property as MatLab behaves strange with respect to tabs for some reason.
Change in 0.6:
(Daniel Wirtz, 2012-06-11)
  • Added a new property NumRows that returns the number of rows (excluding the header if set).
  • Made the output a bit nicer and supporting logical values now
Change in 0.6:
(Daniel Wirtz, 2012-05-04)
  • Added a property PrintTable.HasRowHeader that allows to use a single format specification for all row entries but the first one. Added a test for that.
  • A caption with sprintf-compatible arguments can be passed directly to the PrintTable constructor now.
  • Added support for pdf export (requires pdflatex on PATH)
  • The saveToFile method either opens a save dialog to pick a file or takes a filename.
  • New property PrintTable.TightPDF that determines if the pdf file generated for a table should be cropped to the actual table size or inserted into a standard article document. Having a tight pdf removes the caption of the table.
Change in 0.6:
(Daniel Wirtz, 2011-12-14) Added support for arrays of PrintTable instances in display, print and saveToFile methods.
New in 0.6:
(Daniel Wirtz, 2011-12-01)
  • Added support for LaTeX output
  • New properties PrintTable.Format and PrintTable.Caption
  • Optional caption can be added to the Table
  • Some improvements and fixed display for some special cases
  • New PrintTable.clear method
  • Updated the documentation and test case
New in 0.6:
(Daniel Wirtz, 2011-11-17) Added this class.

This class has originally been developed as part of the framework

KerMor - Model Order Reduction using Kernels

Copyright (c) 2011, Daniel Wirtz All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted only in compliance with the BSD license, see http://www.opensource.org/licenses/bsd-license.php

Todo:
replace fprintf by sprintf and build string that can be returned by this.print and if no output argument is collected directly print it.

Definition at line 17 of file PrintTable.m.

Public Member Functions

 PrintTable (char caption,cell varargin)
 Creates a new PrintTable instance. More...
 
function  display ()
 Overload for the default builtin display method. More...
 
function  print (integer outfile)
 Prints the current table to a file pointer. More...
 
function char str = toString ()
 Returns a character array representation of the current table. More...
 
function  saveToFile (char filename,logical openfile)
 Prints the current table to a file. More...
 
function  addMatrix (titles, data, varargin)
 
function  addRow (varargin)
 Adds a row to the current table. More...
 
function  clear ()
 Clears the current PrintTable contents and caption. More...
 
function  removeRow (integer idx)
 Removes a row from the PrintTable. More...
 
function this = sort (integer colnr,char direction)
 Sorts this table in alphanumeric order. More...
 
function
transposed = 
ctranspose ()
 
function copy = clone ()
 Returns a new instance of PrintTable with the same content. More...
 
function joined = append (PrintTable table,rowvec< integer > columns)
 Appends the cell contents from another table to this table. More...
 

Static Public Member Functions

static function [
res ,
double t ] = 
test_PrintTable ()
 A simple test for PrintTable. More...
 
static function [
res ,
double t ] = 
test_PrintTable_Misc ()
 A simple test for PrintTable. More...
 
static function [
res ,
double t ] = 
test_PrintTable_LaTeX_Export ()
 A simple test for PrintTable. More...
 
static function [
res ,
double t ] = 
test_PrintTable_Failed_LaTeX_Export ()
 

Public Attributes

integer TabCharLen = PrintTable.getDefaultTabCharLen
 Equivalent length of a tab character in single-space characters. More...
 
char ColSep = " | "
 A char sequence to separate the different columns. More...
 
logical HasHeader = false
 Flag that determines if the first row should be used as table header. More...
 
logical HasRowHeader = false
 Flag that determines if there is a row header for the table. More...
 
enum Format = "txt"
 The output format of the table when using the print method. More...
 
char Caption = ""
 A caption for the table. More...
 
logical TightPDF = true
 Flag that indicates if exported tables in PDF format should be sized to the actual table size or be contained in a normal unformatted article page. More...
 
logical StripInsertedTabChars = true
 Flag that determines if inserted tab characters are stripped from any string to preserve the correct display of the PrintTable. More...
 
integer NumRows
 The number of rows in the current table. More...
 
logical TexMathModeDetection
 Flag that tells the PrintTable to surround numerical values with dollar signs "$$" to obtain a more suitable LaTeX number representation. More...
 
 data
 The string cell data. More...
 
 mathmode
 Flag matrix to indicate if a cell value is numeric or not. More...
 
 contlen
 Maximum content length for each colummn. More...
 
 haslatexcommand = false
 Flag to maximize user convenience; this is set whenever a row adding resulted in a string containing a backslash, indicating that latex commands are used. If so, a warning is issued on tex export if TexMathModeDetection is switched off. More...
 
- Public Attributes inherited from handle
 addlistener
 Creates a listener for the specified event and assigns a callback function to execute when the event occurs. More...
 
 notify
 Broadcast a notice that a specific event is occurring on a specified handle object or array of handle objects. More...
 
 delete
 Handle object destructor method that is called when the object's lifecycle ends. More...
 
 disp
 Handle object disp method which is called by the display method. See the MATLAB disp function. More...
 
 display
 Handle object display method called when MATLAB software interprets an expression returning a handle object that is not terminated by a semicolon. See the MATLAB display function. More...
 
 findobj
 Finds objects matching the specified conditions from the input array of handle objects. More...
 
 findprop
 Returns a meta.property objects associated with the specified property name. More...
 
 fields
 Returns a cell array of string containing the names of public properties. More...
 
 fieldnames
 Returns a cell array of string containing the names of public properties. See the MATLAB fieldnames function. More...
 
 isvalid
 Returns a logical array in which elements are true if the corresponding elements in the input array are valid handles. This method is Sealed so you cannot override it in a handle subclass. More...
 
 eq
 Relational functions example. See details for more information. More...
 
 transpose
 Transposes the elements of the handle object array. More...
 
 permute
 Rearranges the dimensions of the handle object array. See the MATLAB permute function. More...
 
 reshape
 hanges the dimensions of the handle object array to the specified dimensions. See the MATLAB reshape function. More...
 
 sort
 ort the handle objects in any array in ascending or descending order. More...
 

Constructor & Destructor Documentation

PrintTable.PrintTable ( char  caption,
cell  varargin 
)

Creates a new PrintTable instance.

Parameters
captionThe caption of the table. Default: '
vararginIf given, they will be passed to sprintf using the specified caption argument to create the table's Caption. Default: []

Definition at line 447 of file PrintTable.m.

References Caption, and clear().

Referenced by clone().

Here is the call graph for this function:

Here is the caller graph for this function:

Member Function Documentation

function PrintTable.addMatrix (   titles,
  data,
  varargin 
)

Definition at line 603 of file PrintTable.m.

References addRow(), data, and k.

Here is the call graph for this function:

function PrintTable.addRow (   varargin)

Adds a row to the current table.

Parameters
vararginAny number of arguments >= 1, each corresponding to a column of the table. Each argument must be a char array.

Definition at line 611 of file PrintTable.m.

References contlen, data, haslatexcommand, and mathmode.

Referenced by addMatrix(), and testing.Speed.BinaryvsMatSave().

Here is the caller graph for this function:

function joined = PrintTable.append ( PrintTable  table,
rowvec< integer columns 
)

Appends the cell contents from another table to this table.

Parameters
tableThe other table
columnsThe column indices to transfer. Default: all
Required fields of table:
Generated fields of joined:

Definition at line 778 of file PrintTable.m.

References Caption, clone(), contlen, data, HasHeader, haslatexcommand, k, mathmode, and NumRows.

Here is the call graph for this function:

function PrintTable.clear ( )

Clears the current PrintTable contents and caption.

Definition at line 654 of file PrintTable.m.

References Caption, contlen, and data.

Referenced by PrintTable().

Here is the caller graph for this function:

function copy = PrintTable.clone ( )

Returns a new instance of PrintTable with the same content.

Generated fields of copy:

Definition at line 755 of file PrintTable.m.

References Caption, ColSep, contlen, data, Format, HasHeader, haslatexcommand, HasRowHeader, mathmode, PrintTable(), StripInsertedTabChars, and TightPDF.

Referenced by append(), and ctranspose().

Here is the call graph for this function:

Here is the caller graph for this function:

function transposed = PrintTable.ctranspose ( )

Definition at line 740 of file PrintTable.m.

References clone(), data, k, mathmode, handle.reshape, and t.

Here is the call graph for this function:

function PrintTable.display ( )

Overload for the default builtin display method.

Calls print with argument 1, i.e. standard output.

Definition at line 468 of file PrintTable.m.

References print().

Here is the call graph for this function:

function PrintTable.print ( integer  outfile)

Prints the current table to a file pointer.

See Also
fprintf
Parameters
outfileThe file pointer to print to. Must either be a valid MatLab fileID or can be 1 or 2, for stdout or stderr, respectively. Default: 1

Definition at line 481 of file PrintTable.m.

References t.

Referenced by display(), saveToFile(), and toString().

Here is the caller graph for this function:

function PrintTable.removeRow ( integer  idx)

Removes a row from the PrintTable.

Parameters
idxThe index of the row to remove

Definition at line 665 of file PrintTable.m.

References data.

function PrintTable.saveToFile ( char  filename,
logical  openfile 
)

Prints the current table to a file.

If a file name is specified, the format is determined by the file extension. Allowed types are "txt" for plain text, "tex" for a LaTeX table and "pdf" for an immediate export of the LaTeX table to a PDF document.

Note
The last option required "pdflatex" to be available on the system's environment.
Parameters
filenameThe file to print to. If the file exists, any contents are discarded. If the file does not exist, an attempt to create a new one is made. Default: Prompts for saving target
openfileFlag that indicates if the exported file should be opened after saving. Default: false

Definition at line 522 of file PrintTable.m.

References Caption, Format, and print().

Here is the call graph for this function:

function this = PrintTable.sort ( integer  colnr,
char  direction 
)

Sorts this table in alphanumeric order.

Optionally, a column number and sort direction can be specified.

The default sort column is one. If you set HasHeader to true, the default sort column will be two. However, the column numbering will always include the first column in order to explicitly allow sorting by row headers.

Parameters
colnrThe column number Default: 1 or 2 depending on HasHeader setting
directionThe sort direction ascend or descend Default: ascend
Generated fields of this:

Definition at line 680 of file PrintTable.m.

References data, HasHeader, k, and mathmode.

function [ res , double t ] = PrintTable.test_PrintTable ( )
static

A simple test for PrintTable.

Generated fields of t:

Definition at line 1257 of file PrintTable.m.

static function [res , doublet ] = PrintTable.test_PrintTable_Failed_LaTeX_Export ( )
static

Definition at line 1427 of file PrintTable.m.

function [ res , double t ] = PrintTable.test_PrintTable_LaTeX_Export ( )
static

A simple test for PrintTable.

Generated fields of t:

Definition at line 1388 of file PrintTable.m.

function [ res , double t ] = PrintTable.test_PrintTable_Misc ( )
static

A simple test for PrintTable.

Generated fields of t:

Definition at line 1326 of file PrintTable.m.

function char str = PrintTable.toString ( )

Returns a character array representation of the current table.

Return values
strThe table as string

Definition at line 507 of file PrintTable.m.

References print().

Here is the call graph for this function:

Member Data Documentation

PrintTable.Caption = ""

A caption for the table.

Depending on the output the caption is added: plain: First line above table tex: Inserted into the \caption command

Default: '

Note
This property has custom functionality when its value is changed.

Definition at line 316 of file PrintTable.m.

Referenced by append(), clear(), clone(), data.ApproxTrainData.getErrorsFor(), PrintTable(), and saveToFile().

PrintTable.ColSep = " | "

A char sequence to separate the different columns.

Default: |

Note
This property has custom functionality when its value is changed.

Definition at line 256 of file PrintTable.m.

Referenced by clone().

PrintTable.contlen

Maximum content length for each colummn.

Note
This property has non-standard access specifiers: SetAccess = Private, GetAccess = Public
Matlab documentation of property attributes.

Definition at line 414 of file PrintTable.m.

Referenced by addRow(), append(), clear(), and clone().

PrintTable.data

The string cell data.

Note
This property has non-standard access specifiers: SetAccess = Private, GetAccess = Public
Matlab documentation of property attributes.

Definition at line 388 of file PrintTable.m.

Referenced by addMatrix(), addRow(), append(), clear(), clone(), ctranspose(), removeRow(), and sort().

PrintTable.Format = "txt"

The output format of the table when using the print method.

Hide the valid format pdf as its only used internally .. i know .. lazyness :-)

Currently the values txt for plaintext and tex for LaTeX output are available.

Default: txt

Note
This property has custom functionality when its value is changed.

Definition at line 303 of file PrintTable.m.

Referenced by clone(), and saveToFile().

PrintTable.HasHeader = false

Flag that determines if the first row should be used as table header.

If true, a separate line with dashes will be inserted after the first printed row.

Default: false

Note
This property has custom functionality when its value is changed.

Definition at line 266 of file PrintTable.m.

Referenced by append(), clone(), sort(), and testing.Speed.TryCatch().

PrintTable.haslatexcommand = false

Flag to maximize user convenience; this is set whenever a row adding resulted in a string containing a backslash, indicating that latex commands are used. If so, a warning is issued on tex export if TexMathModeDetection is switched off.

Note
This property has non-standard access specifiers: SetAccess = Private, GetAccess = Public
Matlab documentation of property attributes.
Default: false

Definition at line 425 of file PrintTable.m.

Referenced by addRow(), append(), and clone().

PrintTable.HasRowHeader = false

Flag that determines if there is a row header for the table.

If true, this causes a special behaviour regarding formatting the the first argument passed to PrintTable.addRow. Let \(n\) be the total number of row content arguments.

  • No format is given: The first argument will be "stringyfied" as all other elements
  • A cell with \(n=1\) format strings, i.e. {f}, is given: This format will be applied to all but the first argument of addRow. This is the actual reason why this flag was introduced, in order to be able to specify a current row string header and set the format for all other entries using one format string.
  • A cell with \(n-1\) format strings is given: They will be applied to the 2nd until last argument of addRow (in that order)
  • A cell with \(n\) format strings is given: They will be applied to all content arguments including the row header (in that order)

    Default: false

Note
This property has custom functionality when its value is changed.

Definition at line 278 of file PrintTable.m.

Referenced by clone(), and models.BaseFirstOrderSystem.getParamInfo().

PrintTable.mathmode

Flag matrix to indicate if a cell value is numeric or not.

Used for TeX output.

See Also
TexMathModeDetection
Note
This property has non-standard access specifiers: SetAccess = Private, GetAccess = Public
Matlab documentation of property attributes.

Definition at line 399 of file PrintTable.m.

Referenced by addRow(), append(), clone(), ctranspose(), and sort().

PrintTable.NumRows

The number of rows in the current table.

Note
This property has the MATLAB attribute Dependent set to true.
Matlab documentation of property attributes.

Definition at line 357 of file PrintTable.m.

Referenced by append().

PrintTable.StripInsertedTabChars = true

Flag that determines if inserted tab characters are stripped from any string to preserve the correct display of the PrintTable.

Affects both directly set strings and those produced by providing a format pattern.

Default: true

Definition at line 342 of file PrintTable.m.

Referenced by clone().

PrintTable.TabCharLen = PrintTable.getDefaultTabCharLen

Equivalent length of a tab character in single-space characters.

Default: PrintTable.DefaultTabCharLen

See Also
PrintTable.getDefaultTabCharLen

Definition at line 244 of file PrintTable.m.

PrintTable.TexMathModeDetection

Flag that tells the PrintTable to surround numerical values with dollar signs "$$" to obtain a more suitable LaTeX number representation.

Set to false to produce plain output of cell contents "as they are".

Default: true

Note
This property has the MATLAB attribute Dependent set to true.
Matlab documentation of property attributes.

Definition at line 370 of file PrintTable.m.

PrintTable.TightPDF = true

Flag that indicates if exported tables in PDF format should be sized to the actual table size or be contained in a normal unformatted article page.

Default: true

Definition at line 330 of file PrintTable.m.

Referenced by clone().


The documentation for this class was generated from the following file: