File Exchange

image thumbnail

table2char(data,var​argin)

version 1.7.0.0 (13.3 KB) by David J. Mack
Converts a table or dataset to a formatted character matrix.

0 Downloads

Updated 07 Sep 2017

View License

Helper function which converts a table/dataset to a compactly formatted character representation (in analogy to NUM2STR). It also makes the table more human readable by only showing the relevant digits, removing unnecessary zeros, the boldfont control commands added in the TABLE display and more.
The function can handle multi-dimensional columns of many types. The string representation is useful for storing the table in a text file or appending it to other text data (e.g. as UserData in a figure).

Comments and Ratings (5)

Oleg Komarov

You should just incorporate the relevant assumptions from this comment in the description of your function. That would make it easier for others to understand the objective you achieve with your submission (which is now much clearer).

Happy holidays

Dear Oleg,
If you do work with humans, then a more readable format is often quite advantageous. The EVALC+REGEXP solution was the starting point of why I wrote this function, since I didn't considered it a very elegant solution. The output of this construct is not formatted in a consistent (and readable) way. My use-case for tables is most often the comparison of different measurements (e.g. rows) of several parameters (e.g. cols). Therefore, within-row differences should be easily perceivable. To achieve this, TABLE2CHAR computes the least needed decimal places over the whole column to make it consistent and potential differences quickly perceivable (just like %g would do for a scalar). It does not "kill decimals" - when your scale is large (e.g. 2 decades) there is no point in showing additional decimal places.
In the same line of thought, there is also no no point in distinguishing the class of a TABLE variable for analysis purposes (thus removing the quotes.

In addition, using EVALC is not encouraged (probably even more than using .\d with numbers).
In summary, you could consider this function as a visualization in a Tufte sense - removing unnecessary ink.
Be aware, that this function is not intended to replace the TABLE.DISP which is fine during coding (except if you create an extremely artificial TABLE where it used to crash).

Cheers, David

Oleg Komarov

a = evalc('disp(T)');
a = regexprep(a,'</?strong>','');

I guess you meant compact format, because your format is definitely inconsistent with Matlab types. The quotes are there to distinguish between cellstrings and chars. Killing the decimals makes you think it's an int and no zero in front of a . is just usually not recommended.

Using evalc('disp(T);'); will neither remove the boldfont control commands, nor will it format each column to a consistent format.

Oleg Komarov

You can simply use evalc()

Updates

1.7.0.0

- [MOD] Major code mod. Outsourced several routines to sub-functions to make the code more maintainable. Changed the style to adhere to Matlab Code Rules.
- [FIX] Error when a variable name is shorter than its unit.

1.6.0.0

[FIX] Error when printing row names & units

1.5.0.0

[FIX] Error for columns containing only 1 finite negative and otherwise NaNs.

1.4.0.0

Added the updated file.

1.4.0.0

[FIX] Rounding to one problem.

1.3.0.0

[ADD] Added the optional input 'AddUnits' to specify if the variables' units should be added to the output.

1.2.0.0

FIX: Disappearing digits when rounding to a column's precision increased the number of integer digits.

1.2.0.0

Corrected a typo.

1.2.0.0

Forgot to update the file.

1.2.0.0

FIX: Further fix for the rounding to zero problem.

1.1.0.0

Now shows if a value is 0 after rounding to the column's precision.

MATLAB Release Compatibility
Created with R2012a
Compatible with any release
Platform Compatibility
Windows macOS Linux