| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Symbolic Math Toolbox |
| Contents | Index |
| Learn more about Symbolic Math Toolbox |
g = matlabFunction(f)
g = matlabFunction(f1, f2, ...)
g = matlabFunction(f,param1,value1,...)
g = matlabFunction(f) converts the symbolic expression f to a MATLAB function with the handle g.
g = matlabFunction(f1, f2, ...) converts a list of the symbolic expressions f1, f2, ... to a MATLAB function with multiple outputs. The function handle is g.
g = matlabFunction(f,param1,value1,...) converts the symbolic expression f to a MATLAB function with the handle g. The command accepts the following options for parameter/value pairs:
Parameter = 'file' allows you to generate an optimized M-file that can accept double or matrix arguments and evaluate the symbolic expression applied to the arguments. Optimized means intermediate variables are automatically generated to simplify or speed the code. value should be a string representing the path to the M-file. If the string is empty, matlabFunction generates an anonymous function. If the string does not end in ".m", the function appends ".m".
Parameter = 'outputs' allows you to set the names of the output variables. value should be a cell array of strings. The default names of output variables coincide with the names you use calling matlabFunction. If you call matlabFunction using an expression instead of individual variables, the default names of output variables consist of the word out followed by the number, for example, out3.
Parameter = 'vars' allows you to set the order of the input variables or symbolic vectors in the resulting function handle or M-file. The default order is alphabetical. value should be either a cell array of strings or symbolic arrays, or a vector of symbolic variables. The number of value entries should equal or exceed the number of free variables in the symbolic expression f.
Note To convert a MuPAD expression or function to a MATLAB function, use f = evalin(symengine,'MuPAD_Expression') or f = feval(symengine, 'MuPAD_Function',x1,...,xn). matlabFunction cannot correctly convert some MuPAD expressions to MATLAB functions. These expressions do not trigger an error message. When converting a MuPAD expression or function that is not on the MATLAB vs. MuPAD Expressions list, always check the results of conversion. To verify the results, execute the resulting function. |
Symbolic Math Toolbox with a Maple engine does not support matlabFunction. For details, see Differences in Functionality When Using MuPAD and Maple Engines.
syms x y r = sqrt(x^2 + y^2); ht = matlabFunction(sin(r)/r)
ht =
@(x,y)sin(sqrt(x.^2+y.^2)).*1./sqrt(x.^2+y.^2)The following example generates a file:
syms x y z r = x^2 + y^2 + z^2; f = matlabFunction(log(r)+r^(-1/2),'file','myfile');
If the file myfile.m already exists in the current directory, matlabFunction replaces the existing function with the converted symbolic expression. You can open and edit the resulting file:
function out1 = myfile(x,y,z) %MYFILE % OUT1 = MYFILE(X,Y,Z) % This function was generated % by the Symbolic Math Toolbox version 5.2. % 07-Nov-2008 14:38:59 t2 = x.^2; t3 = y.^2; t4 = z.^2; t5 = t2 + t3 + t4; out1 = log(t5) + 1./t5.^(1./2);
You can change the order of the input variables:
syms x y z r = x^2 + y^2 + z^2; matlabFunction(r, 'file', 'new_function',... 'vars', [y z x]);
The created new_function accepts variables in the required order:
function r = new_function(y,z,x) %NEW_FUNCTION % R = NEW_FUNCTION(Y,Z,X) % This function was generated % by the Symbolic Math Toolbox version 5.2. % 31-Oct-2008 18:13:19 r = x.^2 + y.^2 + z.^2;
You can specify that the input arguments are vectors:
syms x y z t
r = x^2 + y^2 + z^2;
matlabFunction(r, 'file', 'new_function',...
'vars', {t, [x y z]});The resulting function operates on vectors:
function r = new_function(t,in2) %NEW_FUNCTION % R = NEW_FUNCTION(T,IN2) % This function was generated % by the Symbolic Math Toolbox version 5.2. % 03-Nov-2008 18:19:37 x = in2(:,1); y = in2(:,2); z = in2(:,3); r = x.^2 + y.^2 + z.^2;
You can name of the output variables:
syms x y z
r = x^2 + y^2 + z^2;
q = x^2 - y^2 - z^2;
f = matlabFunction(r, q, 'file', 'new_function',...
'outputs', {'name1','name2'});The generated function returns name1 and name2:
function [name1,name2] = new_function(x,y,z)
%NEW_FUNCTION
% [NAME1,NAME2] = NEW_FUNCTION(X,Y,Z)
% This function was generated
% by the Symbolic Math Toolbox version 5.2.
% 31-Oct-2008 18:20:48
t9 = x.^2;
t10 = y.^2;
t11 = z.^2;
name1 = t10 + t11 + t9;
if nargout > 1
name2 = t9 - t11 - t10;
end
Also, you can convert MuPAD expressions:
syms x y; f = evalin(symengine, 'arcsin(x) + arccos(y)'); matlabFunction(f, 'file', 'new_function');
The created file contains the same expressions written in the MATLAB language:
function f = new_function(x,y) %NEW_FUNCTION % F = NEW_FUNCTION(X,Y) % This function was generated % by the Symbolic Math Toolbox version 5.2. % 31-Oct-2008 17:41:12 f = asin(x) + acos(y);
ccode, fortran, subs, sym2poly, emlBlock
Generating Code from Symbolic Expressions
![]() | log2 (sym) | mfun | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |