File Exchange

image thumbnail

String representation of any data type

version 1.6 (7.24 KB) by Rody Oldenhuis
Generate string representations of all MATLAB data types and user-defined classes.


Updated 02 May 2020

GitHub view license on GitHub

TOSTRING implements an often-requested feature that is not built into MATLAB: represent some data as a string.
Supported and tested data types (of any dimension):
- double
- single
- logical
- char
- (u)int[X]
- cell
- struct
- function_handle
- user-implemented classes
The default string representation is as verbose as possible, meaning that the contents of structure fields, cell array entries, etc. are represented in fully expanded form.
S = TOSTRING(A, 'disp') produces a string representation that is identical to what the command 'disp(A)' would produce.

S = TOSTRING(A, 'compact') or S = TOSTRING(A, N) (with N a positive integer) limits the number of digits displayed in numerical arrays to either 4 ('compact') or N. A warning is issued when applied to a non-numeric array. Note that this setting also affects how enumeration classes are displayed; when this option is used, only the currently selected enumeration value will be shown, rather than the complete class.

Please report any bugs, suggestions or requests to one of the e-mail addresses mentioned in the function header.

Cite As

Rody Oldenhuis (2020). String representation of any data type (, GitHub. Retrieved .

Comments and Ratings (0)



See release notes for this release on GitHub:

Description update

[linked to Github]

Small doc change to better clarify scope of the function

- Removed constructor from enum types, as it cannot be called directly
- Made enum types respond differently to "compact" notation

- 2 bugs related to treatment of integers (see changelog)

Just forgot to update the description field in the field above; fixed.

- Improvements to the display of enumerated types as kindly suggested by Clark Williams (thank you!)
- Implemented support for sparse matrices

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