Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

output::fence

Put delimiters around multiline strings

MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

Syntax

output::fence(left, right, string, <width, <base>>)

Description

output::fence(left, right, string) encloses the multiline string in the delimiters indicated by left and right

You can use output::fence in combination with strprint to overload print (and thereby, standard screen output) for pretty-printing. Note that it does not affect typeset output.

Examples

Example 1

Set TEXTWIDTH to 75:

TEXTWIDTH := 75:

First, define a domain that uses output::fence for output:

domain Fence
  print := x -> output::fence("(", ")", extop(x, 1));
  new   := x -> new(dom, x);
end_domain:

Fence expects a string in its constructor. The output uses output::fence to put parentheses around the input:

print(Plain, Fence("abc"))
(abc)

Strings of height two are only partly placed inside parentheses, for consistency with the pretty-printer:

print(Plain, Fence("abc\ndef"), sin(x^2))
 abc        2
(def), sin(x )

Strings of height more than two are fully bracketed:

print(Plain, Fence("abc\ndef\nghi"))
/ abc \
| def |
\ ghi /

Example 2

Set TEXTWIDTH to 75:

TEXTWIDTH := 75:

The next step in using output::fence is to enclose expressions in parentheses. For this, the information from strprint is useful:

domain FenceExpr
  print := proc(x)
             local str, h1, w1, h, w, b;
           begin
             [str, h1, w1, h, w, b] := strprint(All, extop(x));
             output::fence("{", "]", str, w, b);
           end_proc;
  new   := x -> new(dom, x);
end_domain:

The sixth operand of the return value of strprint(All, …) must be given to output::fence to align baselines properly:

print(Plain, FenceExpr(x), FenceExpr(x^2), FenceExpr(x^2/2*y))
           {  2   --
       2   { x  y  |
{x], {x ], { ----  |
           {   2  --

strprint reacts to TEXTWIDTH and can return a string consisting or more than one logical line. In this case, fencing the returned string leads to strange results:

print(Plain, FenceExpr(_plus(x.i $ i = 0..30)))
{ x0 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12 + x13 \
   --
{                                                                         \
    |
{    + x14 + x15 + x16 + x17 + x18 + x19 + x20 + x21 + x22 + x23 + x24 + x\
25  |
{                                                                         \
    |
{    + x26 + x27 + x28 + x29 + x30                                        \
   --

Here, the string with added delimiter symbols is too wide for TEXTWIDTH. Also, output::fence does not let you control line breaks. Therefore, it is a good practice to avoid putting large delimiters to the left and right of long strings. For example, abs prints in functional notation for long arguments:

print(Plain, abs(_plus(x.i $ i = 0..30)))
abs(x0 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 + x12

   + x13 + x14 + x15 + x16 + x17 + x18 + x19 + x20 + x21 + x22 + x23 + x24

   + x25 + x26 + x27 + x28 + x29 + x30)

Parameters

left, right

Strings indicating the type of delimiter: "(", ")", "[", "]", "[+", "+]", "{", "}", "|", "| ", or " |".

string

The string to enclose

width

The width of the string to enclose. Defaults to the width of the widest line in string.

base

The baseline of the string, counted from the first line. Defaults to the bottom line of the string. If set to -1, the baseline is vertically centered.

Return Values

String

See Also

MuPAD Functions

Was this topic helpful?