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.


Print objects to the screen

For printing in MATLAB®, see print. For displaying a value, see display.

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.


print(<Unquoted>, <NoNL>, <KeepOrder>, <Plain>, <Typeset>, object1, object2, …)


print(object) displays object on the screen.

At interactive level, the result of a MuPAD® command entered at the command prompt is usually displayed on the screen automatically. print serves to generate additional output from within loops or procedures.

Apart from some exceptions mentioned below, the output generated by print is identical to the usual output of MuPAD results at interactive level.

print evaluates its arguments sequentially from left to right (cf. Example 3) and displays the results on the screen. The individual outputs are separated by commas. A new line is started at the end of the output if this is not suppressed by the option NoNL.

The output width for print with option Plain is limited by the environment variable TEXTWIDTH. Cf. Example 4.

With option Plain the style of the output is determined by the value of the environment variable PRETTYPRINT. Cf. Example 5.

print descends recursively into the operands of an object. For each subobject s, print first determines its domain typeT. If the domain T has a "print" slot, then print issues the call T::print(s) to the slot routine. In contrast to the overloading mechanism for most other MuPAD functions, print processes the result of this call recursively, and the result of the recursive process is printed at the position of s (cf. Example 6).

    Note:   The result returned by the "print" method must not contain the domain element s itself as a subobject, since this leads to infinite recursion (cf. Example 7). The same remark also applies to the output procedures of function environments (see below).

If T is a library domain without a "print" slot and the internal operands of s are op1, op2, ..., then s is printed as new(T, op1, op2, ...). (See Example 6.)

"print" methods may return strings or expressions. Strings are always printed unquoted. Expressions are printed in normal mode. If they contain strings, they will be printed with quotation marks. Cf. Example 8.

The output of an expression is determined by the 0th operand of the expression. If the 0th operand is a function environment, then its second operand handles the output of the expression. See Example 9. Otherwise, the expression is printed in functional notation.

In contrast to the usual output of MuPAD objects at interactive level, print does not perform resubstitution of aliases (see Pref::alias for details). Moreover, the routines defined via Pref::output and Pref::postOutput are not called by print. Cf. Example 14.

The output of floating-point numbers depends on the environment variable DIGITS and the settings of Pref::floatFormat (exponential or floating-point representation) and Pref::trailingZeroes (printing of trailing zeroes). Cf. Example 16.

Environment Interactions

print is sensitive to the environment variables DIGITS, PRETTYPRINT, and TEXTWIDTH, and to the output preferences Pref::floatFormat, Pref::keepOrder, and Pref::trailingZeroes.


Example 1

This example shows a simple call of print with strings as arguments. They are printed with quotation marks:

print("Hello", "You"." !"):

Example 2

On platforms supporting typesetting, print can generate typeset output:

print(Typeset, int(f(x)/g(x), x = a..b)):

print uses the Typeset option by default:

print(int(f(x)/g(x), x = a..b)):

ASCII output is available with the option Plain:

print(Plain, int(f(x)/g(x), x = a..b)):
 |  f(x)
 |  ---- dx
/   g(x)

Example 3

Like most other functions, print evaluates its arguments. In the following call, x evaluates to 0 and cos(0) evaluates to 1:

a := 0: print(cos(a)^2):

Use hold if you want to print the expression cos(a)^2 literally:


delete a:

Example 4

print with the option Plain is sensitive to the current value of TEXTWIDTH:

print(Plain, expand((a + b)^4)):
 4      3        2  2        3    4
a  + 4 a  b + 6 a  b  + 4 a b  + b
print(Plain, expand((a + b)^4)):
 4      3        2  2
a  + 4 a  b + 6 a  b

          3    4
   + 4 a b  + b

If you disable the pretty print mode, the print function inserts the line continuation character at the line breaks:

print(Plain, expand((a + b)^4)):
a^4 + 4*a^3*b + 6*a^2*b^\
2 + 4*a*b^3 + b^4

The line continuation character can be invalid for some strings. For example, when you use the code generators, such as generate::MATLAB and generate::Simscape, the displayed code containing the line continuation character is not valid. To avoid inserting this character, change the TEXTWIDTH setting or use the fprint function instead of print:

fprint(Unquoted, 0, expand((a + b)^4))
a^4 + 4*a^3*b + 6*a^2*b^2 + 4*a*b^3 + b^4

Also, see the Example 4 on the fprint help page.


Example 5

print with option Plain is sensitive to the current value of PRETTYPRINT:

print(Plain, a/b):
print(Plain, a/b):
delete old:

Example 6

We demonstrate how to achieve formatted output for elements of a user-defined domain. Suppose that we want to write a new domain Complex for complex numbers. Each element of this domain has two operands: the real part r and the imaginary part s:

Complex := newDomain("Complex"): z := new(Complex, 1, 3):
z + 1;
print(Plain, z + 1):

new(Complex, 1, 3) + 1

Now we want a nicer output for elements of this domain, namely in the form r+s*I, where I denotes the imaginary unit. We implement the slot routine Complex::print to handle this. This slot routine will be called by MuPAD with an element of the domain Complex as argument whenever such an element is to be printed on the screen:

Complex::print := (z -> extop(z, 1) + extop(z, 2)*I):
z + 1;
print(Plain, z + 1):

1 + 3 I + 1
delete Complex, z:

Example 7

The result of a "print" method must not contain the argument as a subobject; otherwise this leads to infinite recursion. In the following example, the slot routine T::print would be called infinitely often. MuPAD tries to trap such infinite recursions and prints `????` instead:

T := newDomain(T): T::print := id:
new(T, 1);
print(Plain, new(T, 1)):
delete T:

Example 8

If a "print" method returns a string, it will be printed unquoted:

Example := newDomain("Example"):  e := new(Example, 1):
Example::print := x -> "elementOfExample":

If a "print"-method returns an expression, it will be printed in normal mode. If the expression contains strings, they will be printed in the usual way with quotation marks:

Example::print := x -> ["elementOfExample", extop(x)]:

delete Example, e:

Example 9

Suppose that you have defined a function f that may return itself symbolically, and you want such symbolic expressions of the form f(x,...) to be printed in a special way. To this end, embed your proceduref in a function environment and supply an output procedure as second argument to the corresponding funcenv call. Whenever an expression of the form f(x,...) is to be printed, the output procedure will be called with the arguments x,... of the expression:

f := funcenv(f,
         proc(x) begin
            if nops(x) = 2 then
              "f does strange things with its arguments ".
              expr2text(op(x, 1))." and ".expr2text(op(x,2))
delete a, b:
f(a, b)/2;
f(a, b, c)/2

delete f:

Example 10

With the option Unquoted, quotation marks are omitted:

print(Unquoted, "Hello", "World"." !"):
Hello, World !

With Unquoted the special characters \t and \n are expanded:

print(Unquoted, "As you can see,\n".
                "'\\n' is the newline character\n".
                "\tand '\\t' a tabulator"):
As you can see,
'\n' is the newline character
  and '\t' a tabulator

Example 11

It is useful to construct output strings using expr2text and the concatenation operator .:

d := 5:  print(Unquoted, "d plus 3 = ".expr2text(d + 3)):
d plus 3 = 8
delete d:

Example 12

With the option NoNL, no new line is put at the end of the output and PRETTYPRINT is implicitly set to FALSE. Apart from that, the behavior is the same as with the option Unquoted:

print(NoNL, "Hello"):  print(NoNL, ",  You"." !\n"):
print(NoNL, "As you can see, PRETTYPRINT is FALSE: "):
print(NoNL, x^2-1):  print(NoNL, "\n"):
,  You !
As you can see, PRETTYPRINT is FALSE: 
x^2 - 1

Example 13

If the option KeepOrder is given, sums are printed in their internal order:

print(b - a):  print(KeepOrder, b - a):

Example 14

Alias resubstitution (see Pref::alias) takes place for normal result outputs in an interactive session, but not for outputs generated by print:

delete a, b: alias(a = b):
a; print(a):

In contrast to the usual result output, print does not react to Pref::output:

old := Pref::output(generate::TeX):
sin(a)^b; print(sin(a)^b):

The same is true for Pref::postOutput:

old := Pref::postOutput("postOutput was called"):
a*b; print(a*b):

postOutput was called

delete old:

Example 15

The output of summands of a sum depends on the form of these summands. If the summand is a _mult expression, only the first and last operand of the product are taken into account for determining the sign of that term in the output. If one of them is a negative number then the "+"-symbol in the sum is replaced by a "-"-symbol:

print(hold(a + b*c*(-2)),
      hold(a + b*(-2)*c),
      hold(a + (-2)*b*c)):

This has to be taken into account when writing "print"-methods for polynomial domains.

Example 16

Floating point numbers are usually printed in fixed-point notation. You can change this to floating-point form with mantissa and exponent via Pref::floatFormat:

print(0.000001, 1000.0): old := Pref::floatFormat("e"):
print(0.000001, 1000.0): Pref::floatFormat(old):

In the default output of floating-point numbers, trailing zeroes are cut off. This behavior can be changed via Pref::trailingZeroes:

print(0.000001, 1000.0): old := Pref::trailingZeroes(TRUE):
print(0.000001, 1000.0): Pref::trailingZeroes(old):
0.000001, 1000.0
0.000001000000000, 1000.000000

The number of digits of floating-point numbers in output depends on the environment variable DIGITS:

DIGITS := 20:  print(float(PI)):
DIGITS := 30:  print(float(PI)):

delete old, DIGITS:

Example 17

The output order of sets differs from the internal order of sets, which is returned by op:

s := {a, b, 1}:
print(Plain, s):

{1, a, b}

The index operator [] can be used to access the elements of a set with respect to the output order:

s[1], s[2], s[3]

delete s:

Example 18

The output of a domain is determined by its "Name" slot if it exists, and otherwise by its key:

T := newDomain("T"):
print(Plain, T):

T::Name := "domain T":
print(Plain, T):

domain T
delete T:

Example 19

It is sometimes desirable to combine strings with "pretty" expressions in an output. This is not possible via expr2text. On the other hand, an output with commas as separators is usually regarded as ugly. The following dummy expression sequence may be used to achieve the desired result. It uses the MuPAD internal function for standard operator output builtin(1100,...), with priority 20—the priority of _exprseq—and with an empty operator symbol "":

myexprseq := funcenv(myexprseq,
                     builtin(1100, 20, "", "myexprseq")):
      myexprseq("String and pretty expression ", a^b, ".")):
String and pretty expression a .
delete myexprseq:

Example 20

If the option Typeset is combined with Unquoted or NoNL, a warning is given and Typeset is ignored:

print(Typeset, Unquoted, "1"):
Warning: Conflicting options, ignoring 'Typeset' [print]

Example 21

For more elaborate constructions, you may want to combine multi-line strings with MuPAD expressions. A first attempt might look like the following:

myexprseq := funcenv(myexprseq,
                     builtin(1100, 20, "", "myexprseq")):
Example := newDomain("Example"):
Example::print :=
  x -> myexprseq("--- \n--\n-\n--\n---", op(x)):
e := new(Example, 1):
print(Plain, e):
"--- \n--\n-\n--\n---"1

Obviously, this approach doesn't work. The return value of the "print" method defined above is not a string, it's a (special) sequence, so the special rules for printing a string do not apply. We would need another domain that simply takes a string and returns exactly this string from its "print" slot. Fortunately, MuPAD already has such a domain, stdlib::Exposed:

Example::print :=
  x -> myexprseq(stdlib::Exposed("--- \n--\n-\n--\n---"),
-   1

For expressions with a higher output, you see that the alignment of the string is constant:

--   1/n
-   x
--  x
-   -
--  y

To change this alignment, replace a \n by \b, thereby making the line it terminates the "baseline" of the string:

Example::print :=
  x -> myexprseq(stdlib::Exposed("--- \b--\n-\n--\n---"),
--- 1 + 2

When multiple \b appear in a string, the first one is taken as defining the base line:

Example::print :=
  x -> myexprseq(stdlib::Exposed("--- \n--\n-\b--\b---"),
-   1 + 2


object1, object2, …

Any MuPAD objects



With this option, character strings are displayed without quotation marks. Moreover, the control characters \n, \t, and \\ in strings are expanded into a new line, a tabulator skip, and a single backslash \, respectively. Cf. Example 10.

The control character \t is expanded with tab-size 8. The following character is placed in the next column i with i mod 8 = 0.

\b is expanded into a newline, too, but when combining multiple strings, the last line with \b at its end is regarded as the "baseline". Cf. Example 21.

    Note:   The option Unquoted implicitly sets the option Plain. If the option Typeset is used together with Unquoted, a warning is given and Typeset is ignored. Cf. Example 20.


This option has the same functionality as Unquoted. In addition, the new line at the end of the output is suppressed. Cf. Example 12.

Moreover, this option implicitly sets PRETTYPRINT to FALSE.

    Note:   The option NoNL implicitly sets the option Plain. If the option Typeset is used together with NoNL, a warning is given and Typeset is ignored. Cf. Example 20.


This option determines the order of terms in sums. Normally, the system sorts the terms of a sum such that a positive term is in the first position of the output. If KeepOrder is given, no such re-ordering takes place and sums are printed in the internal order. Cf. Example 13.

This behavior can also be controlled via Pref::keepOrder. More precisely, the call print(KeepOrder, ...) generates the same output as the following command:



The output is in plain text mode. This is the default behavior in the terminal version. In text mode the value of PRETTYPRINT determines if the output is linear or in a more readable 2D form.


The output is in typesetting mode. This is the default print behavior in the notebook, if no other options are given. The option is only kept for backward compatibility.

In typesetting mode the value of PRETTYPRINT is ignored.

Return Values

print returns the void object null() of type DOM_NULL.

Overloaded By

object1, object2


The output order of sets differs from the internal order of sets, which can be obtained via op. For this reordering in the output, the kernel calls the method DOM_SET::sort, which takes the set as argument and returns a sorted list. The elements of the set are then printed in the order given by this list.

Was this topic helpful?