주요 콘텐츠

formattedDisplayText

Capture display output as string

Description

str = formattedDisplayText(X) returns the Command Window display output of X as a string. The string contains the output disp(X) would generate.

example

str = formattedDisplayText(X,Name,Value) specifies formatting options for the string using one or more Name,Value arguments.

example

Examples

collapse all

Create a 3-by-3 diagonal matrix.

I = diag([1 1 1]);

Use formattedDisplayText to capture the matrix as a string.

strI = formattedDisplayText(I)
strI = 
    "     1     0     0
          0     1     0
          0     0     1
     "

You can also use expressions as inputs.

strIplus = formattedDisplayText(2*I + ones(3))
strIplus = 
    "     3     1     1
          1     3     1
          1     1     3
     "

Create a struct that contains information about a UI element.

S = struct('Type','Button','Size', 45,'Enabled',false)
S = struct with fields:
       Type: 'Button'
       Size: 45
    Enabled: 0

Use formattedDisplayText with the UseTrueFalseforLogical name-value argument to capture the struct as a string with logical values displayed as "true" or "false".

strS = formattedDisplayText(S,'UseTrueFalseForLogical',true)
strS = 
    "       Type: 'Button'
            Size: 45
         Enabled: false
     "

Create a table with customer names, account balances, and amounts of recent deposits.

Customer = ["Rivera";"Park";"Gupta"];
Balance = [5670;868.54;3015];
LastDeposit = [325.10;35.20;70];
T = table(Customer,Balance,LastDeposit)
T =

  3×3 table

    Customer    Balance    LastDeposit
    ________    _______    ___________

    "Rivera"      5670        325.1   
    "Park"      868.54         35.2   
    "Gupta"       3015           70   

Capture the table as a string. Use name-value arguments to set the numeric format to bank style, suppress the boldface markup of the column headings, and remove an extra blank line with compact line spacing.

strT = formattedDisplayText(T,'NumericFormat','bank',...
'SuppressMarkup',true,'LineSpacing','compact')
strT = 

    "    Customer    Balance    LastDeposit
         ________    _______    ___________
         "Rivera"    5670.00      325.10   
         "Park"       868.54       35.20   
         "Gupta"     3015.00       70.00   
     "

SupressMarkup does not suppress formatting in strings it takes as input. For example, change the entry in T for customer "Gupta" to a hyperlink.

T.Customer(3) = string('<a href="https://mathworks.com">Gupta</a>');

Call formattedDisplayText again on the revised table. The boldface formatting of the table headings is suppressed, but the hyperlink contained in the string in the table is not suppressed.

strT = formattedDisplayText(T,'NumericFormat','bank',...
'SuppressMarkup',true,'LineSpacing','compact')
strT = 

    "    Customer    Balance    LastDeposit
         ________    _______    ___________
         "Rivera"    5670.00      325.10   
         "Park"       868.54       35.20   
         "Gupta"     3015.00       70.00   
     "

Input Arguments

collapse all

Input array.

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'NumericFormat','shortE'

Format for numeric values, specified as one of the formats in the table:

Style

Result

Example

'short'

Short, fixed-decimal format with 4 digits after the decimal point.

3.1416

'long'

Long, fixed-decimal format with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values.

3.141592653589793

'shortE'

Short scientific notation with 4 digits after the decimal point.

3.1416e+00

'longE'

Long scientific notation with 15 digits after the decimal point for double values, and 7 digits after the decimal point for single values.

3.141592653589793e+00

'shortG'

Short, fixed-decimal format or scientific notation, whichever is more compact, with a total of 5 digits.

3.1416

'longG'

Long, fixed-decimal format or scientific notation, whichever is more compact, with a total of 15 digits for double values, and 7 digits for single values.

3.14159265358979

'shortEng'

Short engineering notation (exponent is a multiple of 3) with 4 digits after the decimal point.

3.1416e+000

'longEng'

Long engineering notation (exponent is a multiple of 3) with 15 significant digits.

3.14159265358979e+000

'+'

Positive/Negative format with +, -, and blank characters displayed for positive, negative, and zero elements.

+

'bank'

Currency format with 2 digits after the decimal point.

3.14

'hex'

Hexadecimal representation of a binary double-precision number.

400921fb54442d18

'rational'

Ratio of small integers.

355/113

Format for line spacing, specified as 'loose' or 'compact':

Style

Result

Example

'compact'

Suppress excess blank lines to show more output on a single screen.

theta = pi/2
theta =
  1.5708

'loose'

Add blank lines to make output more readable.

theta = pi/2

theta =

  1.5708

Status of display markup, specified as false or true. When the argument is set to false, the output appears exactly as it would in the Command Window, including documentation hyperlinks and boldface on table headers, for example. When the argument is set to true, the output does not show markup. The exception to this rule is when the input array contains strings that contain formatting. In that case, the formatting is preserved even when SuppressMarkup is true.

Logical value display preference, specified as false or true. When the argument is set to false, logical values appear as 1s and 0s. When the argument is set to true, logical values appear as the words "true" and "false".

Version History

Introduced in R2021a

See Also

|