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.


Define a new operator symbol

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.


operator(symb, f, <Prefix | Postfix | Binary | Nary, prio>, <Global>)
operator(symb, Delete, <Global>)


operator(symb, f, T, prio) defines a new operator symbol symb of type T with priority prio. The function f evaluates expressions using the new operator.

operator(symb, Delete) removes the definition of the operator symbol symb.

operator is used to define new user-defined operator symbols or to delete them.

Given the operator symbol "++", say, with evaluating function f, the following expressions are built by the parser, depending on the type of the operator:

  • Prefix:

    The input ++x results in f(x).

  • Postfix:

    The input x++ results in f(x).

  • Binary:

    The input x ++ y ++ z results in f(f(x, y), z).

  • Nary:

    The input x ++ y ++ z results in f(x, y, z).

There may exist operator symbols which are prefixes of other operator symbols. The scanner reads as many characters as possible and chooses the longest matching operator symbol. Cf. Example 3.

It is not possible to define two operators with the same symbol. So one may not define a unary ++ and a binary ++ at the same time.

The following restrictions exist for the operator symbol string symb:

  • It may not be longer than 32 characters.

  • It may not start with a white-space.

  • It may not start with a \ (backslash) character.

Thus, the strings " @" and "/" are not allowed. Please note that currently operator does not check these restrictions.

Builtin operators may be redefined.

It is not possible to define out-fix operators like |x| or 3-nary or other types of operators.

The new operator symbol is also used if files are read, with one exception: if a file is read with the function read using the option Plain, the new operator is not taken into account. (This option is used if MuPAD® library files are read, because otherwise user-defined operators could change the meaning of the source code in an uncontrolled way.)

If the operator is defined while reading a file with option Plain, the definition will be used for the remainder of the file and then be deleted automatically. If the operator is defined with the option Global, this behavior is changed and the operator will not be active while reading the file, but will exist at the interactive level instead.

Environment Interactions

The new operator symbol symb is known by the parser and may be used to enter expressions. The new operator symbol will not be used when reading files using the function read with the option Plain.

The function f corresponding to the new operator will always be converted into a function environment containing an additional output routine for the operator output, unless it contained an output routine already.


Example 1

This example shows how to define an operator symbol for the bit-shift operation (as in the language C):

bitshiftleft := (a, b) -> a * 2^b:
operator("<<", bitshiftleft, Binary, 950):

After this call, the symbol << can be used to enter expressions:

2 << 1, x << y

operator("<<", Delete):

Example 2

Identifiers can be used as operator symbols:

operator("x", _vector_product, Binary, 1000):
print(Plain, a x b x c)
(a x b) x c
operator("x", Delete):

Example 3

This example shows that the scanner tries to match the longest operator symbol:

operator("~", F, Prefix, 1000):
operator("~>", F1, Prefix, 1000):
operator("~~>", F2, Prefix, 1000):
print(Plain, ~~ x, ~~> x, ~ ~> x, ~~~> x)
~ ~ x, ~~> x, ~ ~> x, ~ ~~> x
operator("~", Delete):
operator("~>", Delete):
operator("~~>", Delete):



The operator symbol: a character string.


The function evaluating expressions using the operator.


The priority of the operator: an integer between 1 and 1999. The default is 1300.



The operator is regarded as a unary operator with prefix notation. Given the operator symbol "++" and the evaluation function f, the input ++x is parsed as the expression f(x).


The operator is regarded as a unary operator with postfix notation. Given the operator symbol "++" and the evaluation function f, the input x++ is parsed as the expression f(x).


The operator is regarded as a non-associative binary operator with infix notation. Given the operator symbol "++" and the evaluation function f, the input x ++ y ++ z is parsed as the expression f(f(x, y), z), i.e. the operator binds left-to-right.


The operator is regarded as an associative n-ary operator with infix notation. Given the operator symbol "++" and the evaluation function f, the input x ++ y ++ z is parsed as the expression f(x, y, z).


The operator with symbol symb is deleted


When defining an operator inside library or package code (technically: inside a file which is read with the option Plain), the option Global changes the meaning of the operator definition: Instead of defining an operator for the remainder of the file, it defines an operator for the interactive level.

Return Values

Void object of type DOM_NULL.


When the scanner reads a new token, it first discards any whitespace and backslash characters. Then it tries to match user-defined operator symbols. The longest user-defined operator symbol matching the scanned characters is made the next token. If no user-defined operator symbol matches, it scans for the built-in tokens.

The parser uses both recursive-descend and a operator precedence parsing. Built-in and user-defined operators are parsed using operator precedence.

Was this topic helpful?