Generate MATLAB formatted string
This functionality does not run in MATLAB.
generate::MATLAB(e) generates MATLAB® code for the MuPAD® expression e.
generate::MATLAB returns a MATLAB formatted string representing an expression, equation, list of equations or a matrix.
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 print function. Use the printing option 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 print function breaks that line into several shorter lines. The inserted line continuation character (\) is not valid in MATLAB. To avoid inserting line continuation characters, increase the TEXTWIDTH setting or use the fprint function 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 evalin or feval functions. 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 using evalin or 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.
The 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, generate::MATLAB converts the elements of expressions, equations, and matrices to the double format:
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 = zeros(2,3); A(1,1) = 1.0; A(2,3) = 1.0;
If the first index of an array is not 1, the generate::MATLAB function issues a warning:
A:= array(1..2, 2..3, [[1,2],[3,4]]): print(Unquoted, generate::MATLAB(A))
Warning: The array index 'A[1..2, 2..3]' is out of range 1..n. [DOM_ARRAY::CF]
A = zeros(2,2); A(1,2) = 1.0; A(1,3) = 2.0; A(2,2) = 3.0; A(2,3) = 4.0;
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);
The generate::MATLAB function does not optimize your code:
print(Unquoted, generate::MATLAB([x = a + b, y = (a + b)^2])):
x = a+b; y = (a+b)^2;
You can use the generate::optimize function before converting your MuPAD code to MATLAB syntax. For example, this function can reduce the number of operations by finding common subexpressions:
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 fprint function:
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 resulting file.
By default, the generate::MATLAB function can issue warnings:
Warning: Function 'g' is not verified to be a valid MATLAB function.
t0 = g(x);
If you started using generate::MATLAB recently, the warnings can help you identify the potential issues in the converted code. If you want to suppress warnings, use the NoWarning option:
print(Unquoted, generate::MATLAB(g(x), NoWarning))
t0 = g(x);
generate::MATLAB returns a string containing MATLAB code.