expr2text

Convert objects into character strings

Use only in the MuPAD Notebook Interface.

This functionality does not run in MATLAB.

Syntax

expr2text(object)

Description

expr2text(object) converts object into a character string. The result usually corresponds to the screen output of object when PRETTYPRINT is set to FALSE.

If the function is called without arguments, then an empty character string is created. If more than one argument is given, the arguments are interpreted as an expression sequence and are converted into a single character string.

Like most other MuPAD® function, expr2text evaluates its arguments before the conversion.

If strings occur in object, they will be quoted in the result.

Examples

Example 1

Expressions are converted into character strings:

expr2text(a + b)

expr2text quotes strings. Note that the quotation marks are preceded by a backslash when they are printed on the screen:

expr2text(["text", 2])

Example 2

If more than one argument is given, the arguments are treated as a single expression sequence:

expr2text(a, b, c)

If no argument is given, an empty string is generated:

expr2text()

Example 3

expr2text evaluates its arguments:

a := b: c := d: expr2text(a, c)

Use hold to prevent evaluation:

expr2text(hold(a, c));
delete a, c:

Here is another example:

expr2text((a := b; c := d));
delete a, c:

e := expr2text(hold((a := b; c := d)))

The last string contains a newline character "\n". Use print with option Unquoted to expand this into a new line:

print(Unquoted, e):
(a := b;
c := d)

Example 4

expr2text is overloadable. It uses a default output for elements of a domain if the domain has neither a "print"slot nor an "expr2text"slot:

T := newDomain("T"): e := new(T, 1):
e;
print(e):
expr2text(e)

If a "print" slot exists, it will be called by expr2text to generate the output:

T::print := proc(x) begin
  _concat("foo: ", expr2text(extop(x)))
end_proc:
e;
print(e):
expr2text(e)

If you want expr2text to generate an output differing from the usual output generated by print, you can supply an "expr2text" method:

T::expr2text := proc(x) begin
  _concat("bar: ", expr2text(extop(x)))
end_proc:
e;
print(e):
expr2text(e)

Parameters

object

Any MuPAD object

Return Values

string.

Overloaded By

object

Algorithms

When processing a domain element e, expr2text first tries to call the "expr2text" method of the corresponding domain T. If it exists, T::expr2text(e) is called and the result is returned. If no "expr2text" method exists, expr2text tries to call the "print" method in the same way. If no "print" method exists either, expr2text will generate a default output. Cf. Example 4.

An "expr2text" method or a "print" method may return an arbitrary MuPAD object, which will be processed recursively by expr2text.

    Note:   The returned object must not contain the domain element e as a sub-object. Otherwise, the MuPAD kernel runs into infinite recursion and emits an error message.

For expressions, the result returned by expr2text always coincides with the output produced by print. If the 0th operand of the expression is a function environment, the result of expr2text is computed by the second operand of the function environment.

See Also

MuPAD Functions

Was this topic helpful?