Generate MATLAB formatted string
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.
generate::MATLAB(e) generates MATLAB® code
for the MuPAD® expression
generate::MATLAB returns a MATLAB formatted
string representing an expression, equation, list of equations or
generate::MATLAB assumes that the type of
converted data is
double. See Example 1.
An equation represents an assignment in MATLAB code. See Example 4.
When generating MATLAB code for a matrix, the generator produces a matrix of zeros, and then it substitutes nonzero elements. See Example 2.
To display generated MATLAB code on screen, use the
Unquoted to remove quotation
marks and to expand special characters like line breaks and tabs.
If a generated code line is longer than the
TEXTWIDTH setting, the
\) is not valid in MATLAB. To avoid
inserting line continuation characters, increase the
or use the
to write generated code to a file.
Working from the MATLAB workspace you can create a MATLAB function
containing your expression. To call the MuPAD expression from
the MATLAB workspace, use
See Create MATLAB Functions from MuPAD Expressions.
If you work with the Simulink® products, you can copy the
generated code and paste it into a Simulink block. Also, you
can call the MuPAD expression from the MATLAB workspace
feval functions. Working from the MATLAB workspace
you can automatically create a Simulink block containing your
expression. See Create MATLAB Function Blocks from MuPAD Expressions.
NoWarning option lets you suppress warnings.
See Example 7.
By default, MATLAB stores all numeric values as double-precision
floating-point. In accordance with the default MATLAB data type,
the elements of expressions, equations, and matrices to the
print(Unquoted, generate::MATLAB(x^2 + y/3 + 1/6))
t0 = y*(1.0/3.0)+x^2+1.0/6.0;
The generator produces a matrix of zeros, and then it replaces nonzero elements:
A:= matrix([[1, 0, 0],[0, 0, 1]]): print(Unquoted, generate::MATLAB(A))
A = reshape([1.0,0.0,0.0,0.0,0.0,1.0],[2,3]);
If the first index of an array is not 1, the
issues a warning:
A:= array(1..2, 2..3, [[1,2],[3,4]]): print(Unquoted, generate::MATLAB(A))
Warning: Array index 'A[1..2, 2..3]' out of range 1..n. [DOM_ARRAY::CF]
A = reshape([1.0,3.0,2.0,4.0],[2,2]);
When generating MATLAB code from equations, you get assignments instead of equations. For example, generate MATLAB code for the following list of equations:
f := generate::MATLAB([x = exp(t*s), y = sin(t)*cos(s)]): print(Unquoted, f)
x = exp(s*t); y = cos(s)*sin(t);
generate::MATLAB function does not optimize
print(Unquoted, generate::MATLAB([x = a + b, y = (a + b)^2])):
x = a+b; y = (a+b)^2;
You can use the
before converting your MuPAD code to MATLAB syntax. For
example, this function can reduce the number of operations by finding
f := generate::optimize([x = a + b, y = (a + b)^2]): print(Unquoted, generate::MATLAB(f)):
x = a+b; y = x^2;
To create a file with a MATLAB formatted string representing
a symbolic expression, use the
A:= matrix([[1, 0, 0],[0, 0, 1]]): fprint(Unquoted, Text, "matrixA.m", generate::MATLAB(A))
If the file matrixA.m already exists,
fprint replaces the existing MATLAB code
with the converted symbolic expression. You can open and edit the
By default, the
can issue warnings:
Warning: Function 'g' not verified to be a valid MATLAB function.
t0 = g(x);
If you started using
the warnings can help you identify the potential issues in the converted
code. If you want to suppress warnings, use the
print(Unquoted, generate::MATLAB(g(x), NoWarning))
t0 = g(x);
An expression, equation, list of equations, or a matrix
generate::MATLAB returns a string containing MATLAB code.