Print into string

MuPAD® notebooks will be removed in a future release. 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.


strprint(<All>, <Unquoted>, <NoNL>, <KeepOrder>, object1, object2, …)


strprint(objects) returns the string print(objects) would display on the screen.

strprint returns a string that contains the output print would have sent to the screen for the same arguments. This string contains `\n' characters if the output would have consisted of multiple lines.

On Windows® systems, each \n is preceded by \r, because that is the traditional end-of-line combination since that is the end-of-line combination inherited from CP/M. The examples in this documentation assume a system like UNIX®. Also note that with Typesetting activated, \r, \n, \t, and \b will not be displayed in a string.

All options and dependencies on variables are interpreted as described in the documentation of print. Especially, this means PRETTYPRINT affects the output of strprint. Overloading of print is taken into account. See ?print for details.

Environment Interactions

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


Example 1

The string returned by strprint, when printed with option Unquoted, yields the same output as the operands would have in the first place:

s := strprint(a*x^2-7):
print(Unquoted, s)
a x  - 7

This can be used to combine multiple outputs:

s1 := strprint(a*x^2-7):
s2 := _concat("-" $ TEXTWIDTH)."\n":
s3 := strprint(sin(1/x)):
print(Unquoted, s1.s2.s3)
a x  - 7
   / 1 \
sin| - |
   \ x /

In the example above, you can see that the output of strprint does not contain the spaces usually used for centering. The output in the first example was centered, because it used only a fraction of the text width, while the string s1.s2.s3 in the second example spans the whole width of the line and is therefore printed flush left.

Example 2

For demonstrative purposes, let us write a domain that puts an expression into a box. We make use of the fact that strprint returns strings starting with a newline, of output::fence and of indexed assignment to strings:

domain box
  print := proc(e)
             local ex, str, w;
             save TEXTWIDTH;
             if TEXTWIDTH > 15 then 
               TEXTWIDTH := TEXTWIDTH - 4;
             ex := extop(e, 1);
             str := strprint(All, ex);
             w := str[5]+4;
             str := output::fence("| ", " |", 
                                  str[5], str[6]+1);
             str[1..w]     := "+"._concat("-"$w-2)."+";
             str[-w-1..-2] := "+"._concat("-"$w-2)."+";

   new := x -> new(dom, x);
print(Plain, box(a), box(sin(1/x)))
+---+  |    / 1 \ |
| a |, | sin| - | |
+---+  |    \ x / |
print(Plain, box(box(hold(E=m*c^2))))
| +----------+ |
| |        2 | |
| | E = m c  | |
| +----------+ |

Example 3

As a last example, we implement a print-method for matrices over 5:

M5 := Dom::Matrix(Dom::IntegerMod(5))

The standard output function simply puts “mod 5” behind every entry, inherited from the output method of Dom::IntegerMod(5):

A := M5([[1,2,3,4],[5,6,7,8],[-2,-3,0,1]])

We now replace this method:

M5::print := proc(A)
               local str, h1, w1, h, w, b;
               [str, h1, w1, h, w, b] := strprint(All, expr(A));
               _concat(str, " " $ w, "[mod 5]");
/ 1, 2, 3, 4 \
|            |
| 0, 1, 2, 3 |
|            |
\ 3, 2, 0, 1 /
              [mod 5]

Alternatively, we can set the [mod 5] right beneath the brackets:

M5::print := proc(A)
               local str;
               str := strprint(expr(A));
               str[-1..-1] := " [mod 5]";
/ 1, 2, 3, 4 \
|            |
| 0, 1, 2, 3 |
|            |
\ 3, 2, 0, 1 / [mod 5]


object1, object2, …

Any MuPAD® objects



When the option All is given, strprint returns additional information on the string generated by printing. More specifically, it returns a list consisting of

  1. the formatted string,

  2. the height (in characters) of the first line,

  3. the width of the first line,

  4. the height of the complete string,

  5. the width of the complete string,

  6. the baseline, counted from top to bottom.

Example 2 contains sample code that makes use of this information.


Display character strings without quotation marks and with expanded control characters '\n', '\t', and '\b'.


Like Unquoted, but no newline is put at the end. PRETTYPRINT is implicitly set to FALSE.


Display operands of sums (of type "_plus") always in the internal order.

Return Values

DOM_STRING or a list of a DOM_STRING and five integers.

Overloaded By

See Also

MuPAD Functions