assert_printtable — Prints a table of data with a given number of digits.
strmat = assert_printtable ( table ) strmat = assert_printtable ( table , digits ) strmat = assert_printtable ( table , digits , prntbool ) strmat = assert_printtable ( table , digits , prntbool , sep )
a mxn matrix of doubles, the table of values
a 1x1 matrix of floating point integers, the number of digits to display (default digits=17)
a 1x1 matrix of booleans, prntbool=%t to print the table in the console (default prntbool=%f)
a 1x1 matrix of strings, the separator between the columns (default sep=" ")
a nx1 matrix of strings, the formatted table
Prints a table of data with a given number of digits.
Any optional input argument equal to the empty matrix is replaced by its default value.
Notice that, if the exponent requires 2 digits or less, then the number of displayed digits after the decimal point is digits+1. If the exponent requires 3 digits, then the number of displayed digits after the decimal point is exactly digits. This is a consequence of the inconsistency of the format function.
The goal of this function is to provide a portable way of creating tables of data, for creation of unit tests.
We do not use the msprintf function, because this function produces a different number of digits in the exponent on Windows and on Linux.
This function allows to display values in a more numerically consistent way than the built-in format and disp functions. For example, the number of digits in the format function do not correspond to the number of digits after the decimal point. The printtable function manages this transparently for the user.
The current function also manages the printing more consistently than the sci2exp function, which does not manage the number of digits.
Special IEEE values such as %nan and %inf are displayed as "%inf" and "%nan", so that the output of the printtable function can be directly inserted into a script. This is different from the output of the disp function, where %inf entries are displayed as "Infinity" and %nan entries are displayed as "NaN", making the copy and paste difficult.
Moreover, the formatting of the entries in the matrix is done entry-by-entry, so that values of varying exponents can be managed easily (see the 3.15e300 value in the example below).
table = [ 1 0 200 %inf 0 1 1.e-300 200 %inf 0 9.99999999999990010e-001 9.99999999999999980e-201 200 3.15e300 102 9.99999999899999990e-001 1.e-100 200 296 117 1 %inf -%inf %nan 0 ]; strmat = assert_printtable ( table ) strmat = assert_printtable ( table , 17 ) strmat = assert_printtable ( table , 17 , %t ) strmat = assert_printtable ( table , 17 , %t , "," ) strmat = assert_printtable ( table , 15 ) strmat = assert_printtable ( table , 8 )