Reserved Variable and Function Names

Both MATLAB® and MuPAD® have their own reserved keywords, such as function names, special values, and names of mathematical constants. Using reserved keywords as variable or function names can result in errors. If a variable name or a function name is a reserved keyword in one or both interfaces, you can get errors or incorrect results. If you work in one interface and a name is a reserved keyword in another interface, the error and warning messages are produced by the interface you work in. These messages can specify the cause of the problem incorrectly.

    Tip   The best approach is to avoid using reserved keywords as variable or function names, especially if you use both interfaces.

Conflicts Caused by MuPAD Function Names

In MuPAD, function names are protected. Normally, the system does not let you redefine a standard function or use its name as a variable. (To be able to modify a standard MuPAD function you must first remove its protection.) Even when you work in the MATLAB Command Window, the MuPAD engine handles symbolic computations. Therefore, MuPAD function names are reserved keywords in this case. Using a MuPAD function name while performing symbolic computations in the MATLAB Command Window can lead to an error:

solve('D - 10')

The message does not indicate the real cause of the problem:

Error using solve (line 263)
Specify a variable for which you solve. 

To fix this issue, use the syms function to declare D as a symbolic variable. Then call the symbolic solver without using quotes:

syms D
solve(D - 10)

In this case, the toolbox replaces D with some other variable name before passing the expression to the MuPAD engine:

ans =

To list all MuPAD function names, enter this command in the MATLAB Command Window:

evalin(symengine, 'anames()')

If you work in a MuPAD notebook, enter:


Conflicts Caused by Syntax Conversions

Many mathematical functions, constants, and special values use different syntaxes in MATLAB and MuPAD. See the table MATLAB vs. MuPAD Expressions for these expressions. When you use such functions, constants, or special values in the MATLAB Command Window, the toolbox internally converts the original MATLAB expression to the corresponding MuPAD expression and passes the converted expression to the MuPAD engine. When the toolbox gets the results of computations, it converts the MuPAD expressions in these results to the MATLAB expressions.

Suppose you write MuPAD code that introduces a new alias. For example, this code defines that pow2 computes 2 to the power of x:


Save this code in the program file in the C:/MuPAD folder. Before you can use this code, you must read the program file into the symbolic engine. Typically, you can read a program file into the symbolic engine by using read. This approach does not work for code defining aliases because read ignores them. If your code defines aliases, use feval to call the MuPAD read function. For example, enter these commands in the MATLAB Command Window:

feval(symengine, 'read',' "C:/MuPAD/" ');

Now you can use pow2 to compute 2x. For example, compute 22:

feval(symengine, 'pow2', '2')
ans =

Now suppose you want to introduce the same alias and the following procedure in one program file:


mySum := proc(n)
local i, s;
    s := 0:
    for i from 1 to n do
        s := s + s/i + i

Save this code in the program file in the C:/MuPAD folder. Again, you must read the program file into the symbolic engine, and you cannot use read because the code defines an alias. Enter these commands in the MATLAB Command Window:

feval(symengine, 'read',' "C:/MuPAD/" ');
Error using mupadengine/feval (line 157)
MuPAD error: Error: The aliased identifier is invalid at this place.
[file C:/MuPAD/, line 4, col 7]
  Evaluating: read
  Reading File: C:/MuPAD/

In this example, using the variable i causes the problem. The toolbox treats i as the imaginary unit, and therefore, converts it to I before passing the procedure to the MuPAD engine. Then the toolbox passes the converted code, with all instances of i replaced by I, to the MuPAD engine. This causes an error because I is protected, and the code tries to overwrite its value.

Reading the myProcSum procedure in a MuPAD notebook does not cause an error.

Was this topic helpful?