matlabFunction
Convert symbolic expression to function handle or file
Description
specifies options using one or more name-value arguments in addition to any of the
input argument combinations in the previous syntaxes.ht = matlabFunction(___,Name,Value)
For example, you can specify the File name-value argument to
write the generated MATLAB function to a file. You can also specify the Vars
name-value argument to generate a MATLAB function with input arguments that are a
combination of scalar and vector variables.
Examples
Input Arguments
Name-Value Arguments
Output Arguments
Limitations
Some symbolic functions that have no corresponding MATLAB functions operating on the
doubledata type, such assimplifyandsolve, are kept as symbolic functions in the converted MATLAB function handle or file. The converted file that consists of these functions cannot be deployed using MATLAB Coder™ or MATLAB Compiler™. You need to create your own functions with thedoubledata type to replace these symbolic functions. If you are interested in a symbolic function that cannot be deployed, please contact MathWorks Technical Support.
Tips
When you use the
Filename-value argument, userehashto make the generated function available immediately.rehashupdates the MATLAB list of known files for directories on the search path.If the
Filename-value argument is empty, then MATLAB function returns an anonymous function.If a symbolic expression involves a conditional statement, such as a piecewise condition,
matlabFunctioncan convert the expression to a MATLAB file, but not an anonymous function. The function in the generated file can accept only scalar inputs. For example, convert a piecewise condition to a MATLAB file namedpiecewiseFunc.syms x p = piecewise(x<0, x^2-8, x>=0, -x) matlabFunction(p,"File","piecewiseFunc") y1 = piecewiseFunc(0) y2 = piecewiseFunc(-2)
Use
matlabFunctionto convert one or more symbolic expressions to a MATLAB function and write the resulting function to an M-file. You can then use the generated M-file to create standalone applications and web apps using MATLAB Compiler. For example, see Deploy Generated MATLAB Functions from Symbolic Expressions with MATLAB Compiler.You can also use the generated M-file to create C or C++ code using the MATLAB Coder app. For example, see Generate C Code from Symbolic Expressions Using the MATLAB Coder App.
To generate a MATLAB function with input arguments that are a combination of scalar and vector variables, specify the
Varsname-value argument as a cell array. For examples, see Specify Input Arguments for Generated Function and Generate Function with Vector Input Arguments.
Version History
Introduced in R2008b
See Also
ccode | daeFunction | fortran | rehash | matlabFunctionBlock | odeFunction | simscapeEquation | subs | sym2poly