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).
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).
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).
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.
You can simply use evalc()
- [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 printing row names & units
[FIX] Error for columns containing only 1 finite negative and otherwise NaNs.
Added the updated file.
[FIX] Rounding to one problem.
[ADD] Added the optional input 'AddUnits' to specify if the variables' units should be added to the output.
FIX: Disappearing digits when rounding to a column's precision increased the number of integer digits.
Corrected a typo.
Forgot to update the file.
FIX: Further fix for the rounding to zero problem.
Now shows if a value is 0 after rounding to the column's precision.