Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

matlabFunctionBlock

Convert symbolic expression to MATLAB Function block

Syntax

  • matlabFunctionBlock(block,f)
    example
  • matlabFunctionBlock(block,f1,...,fN)
    example
  • matlabFunctionBlock(___,Name,Value)
    example

Description

example

matlabFunctionBlock(block,f) converts f to a MATLAB® Function block that you can use in Simulink® models. Here, f can be a symbolic expression, function, or a vector of symbolic expressions or functions.

block specifies the name of the block that you create or modify.

example

matlabFunctionBlock(block,f1,...,fN) converts symbolic expressions or functions f1,...,fN to a MATLAB Function block with N outputs. Each element of f1,...,fN can be a symbolic expression, function, or a vector of symbolic expressions or functions.

example

matlabFunctionBlock(___,Name,Value) converts a symbolic expression, function, or a vector of symbolic expressions or functions to a MATLAB Function block using additional options specified by one or more Name,Value pair arguments. You can specify Name,Value after the input arguments used in the previous syntaxes.

Examples

Convert Symbolic Expression

Create a new model and convert a symbolic expression to a MATLAB Function block.

Create a new empty model and open it.

new_system('my_system')
open_system('my_system')

Create a symbolic expression.

syms x y z
f = x^2 + y^2 + z^2;

Use matlabFunctionBlock to create the block my_block containing the symbolic expression. Double-click the generated block to open and edit the function defining the block.

matlabFunctionBlock('my_system/my_block',f)
function f = my_block(x,y,z)
%#codegen
f = x.^2 + y.^2 + z.^2;

If you use the name of an existing block, matlabFunctionBlock replaces the definition of an existing block with the converted symbolic expression.

Save and close my_system:

save_system('my_system')
close_system('my_system')

Convert Symbolic Function

Create a new model and convert a symbolic function to a MATLAB Function block.

Create a new empty model and open it.

new_system('my_system')
open_system('my_system')

Create a symbolic function.

syms x y z
f(x, y, z) = x^2 + y^2 + z^2;

Convert f to a MATLAB Function block. Double-click the block to see the function.

matlabFunctionBlock('my_system/my_block',f)
function f = my_block(x,y,z)
%#codegen
f = x.^2+y.^2+z.^2;

Create Blocks with Multiple Outputs

Convert several symbolic expressions to a MATLAB Function block with multiple output ports.

Create a new empty model and open it.

new_system('my_system')
open_system('my_system')

Create three symbolic expressions.

syms x y z
f = x^2;
g = y^2;
h = z^2;

Convert them to a MATLAB Function block. matlabFunctionBlock creates a block with three output ports. Double-click the block to see the function.

matlabFunctionBlock('my_system/my_block',f,g,h)
function [f,g,h] = my_block(x,y,z)
%#codegen
f = x.^2;
if nargout > 1
    g = y.^2;
end
if nargout > 2
    h = z.^2;
end

Specify Function Name for Generated Function

Specifying the name of the function defining the generated MATLAB Function block.

Create a new empty model and open it.

new_system('my_system')
open_system('my_system')

Create a symbolic expression.

syms x y z
f = x^2 + y^2 + z^2;

Generate a block and set the function name to my_function. Double-click the block to see the function.

matlabFunctionBlock('my_system/my_block',f,...
                    'FunctionName', 'my_function')
function f = my_function(x,y,z)
%#codegen
f = x.^2+y.^2+z.^2;

Disable Code Optimization

When you convert a symbolic expression to a MATLAB Function block, matlabFunctionBlock optimizes the code by default. This approach can help simplify and speed up further computations that use the file. Nevertheless, generating the optimized code from some symbolic expressions and functions can be very time-consuming. Use Optimize to disable code optimization.

Create a new empty model and open it.

new_system('my_system')
open_system('my_system')

Create a symbolic expression.

syms x
r = x^2*(x^2 + 1);

Use matlabFunctionBlock to create the block my_block containing the symbolic expression. Double-click the block to see the function defining the block. By default, matlabFunctionBlock creates a file containing the optimized code.

matlabFunctionBlock('my_system/my_block',r)
function r = my_block(x)
%#codegen
t2 = x.^2;
r = t2.*(t2+1.0);

Disable the code optimization by setting the value of Optimize to false.

matlabFunctionBlock('my_system/my_block',r,...
                    'Optimize',false)
function r = my_block(x)
%#codegen
r = x.^2.*(x.^2+1.0);

Specify Input Ports for Generated Block

Specify the order of the input variables that form the input ports in a generated block.

Create a new empty model and open it.

new_system('my_system')
open_system('my_system')

Create a symbolic expression.

syms x y z
f = x^2 + y^2 + z^2;

Convert the expression to a MATLAB Function block. By default, matlabFunctionBlock uses alphabetical order of input arguments when converting symbolic expressions.

matlabFunctionBlock('my_system/my_block',f)
function f = my_block(x,y,z)
%#codegen
f = x.^2+y.^2+z.^2;

Use the Vars argument to specify the order of the input ports.

matlabFunctionBlock('my_system/my_block',f,...
                    'Vars', [y z x])
function f = my_block(y,z,x)
%#codegen
f = x.^2+y.^2+z.^2;

Specify Output Ports

When generating a block, rename the output variables and the corresponding ports.

Create a new empty model and open it.

new_system('my_system')
open_system('my_system')

Create a symbolic expression.

syms x y z
f = x^2 + y^2 + z^2;

Convert the expression to a MATLAB Function block and specify the names of the output variables and ports. Double-click the block to see the function defining the block.

matlabFunctionBlock('my_system/my_block',f,f + 1,f + 2,...
                    'Outputs', {'name1','name2','name3'})
function [name1,name2,name3] = my_block(x,y,z)
%#codegen
t2 = x.^2;
t3 = y.^2;
t4 = z.^2;
name1 = t2+t3+t4;
if nargout > 1
    name2 = t2+t3+t4+1.0;
end
if nargout > 2
    name3 = t2+t3+t4+2.0;
end

Specify Function Name, Input and Output Ports

Call matlabFunctionBlock using several name-value pair arguments simultaneously.

Create a new empty model and open it.

new_system('my_system')
open_system('my_system')

Create a symbolic expression.

syms x y z
f = x^2 + y^2 + z^2;

Call matlabFunctionBlock using the name-value pair arguments to specify the function name, the order of the input ports, and the names of the output ports. Double-click the block to see the function defining the block.

matlabFunctionBlock('my_system/my_block',f,f + 1,f + 2,...
                    'FunctionName', 'my_function','Vars',[y z x],...
                    'Outputs',{'name1','name2','name3'})
function [name1,name2,name3] = my_function(y,z,x)
%#codegen
t2 = x.^2;
t3 = y.^2;
t4 = z.^2;
name1 = t2+t3+t4;
if nargout > 1
    name2 = t2+t3+t4+1.0;
end
if nargout > 2
    name3 = t2+t3+t4+2.0;
end

Convert MuPAD Expression to MATLAB Function Block

Convert a MuPAD® expression to a MATLAB Function block.

Create a new empty model and open it.

new_system('my_system')
open_system('my_system')

Create a expression written in the MuPAD language.

syms x y
f = evalin(symengine, 'arcsin(x) + arccos(y)');

Convert the expression to a MATLAB Function block The resulting block contains the same expressions written in the MATLAB language:

matlabFunctionBlock('my_system/my_block', f)
function f = my_block(x,y)
%#codegen

f = asin(x) + acos(y);

Related Examples

Input Arguments

collapse all

Block to create of modify, specified as a character vector.

Symbolic input to be converted to MATLAB Function block, specified as a symbolic expression, function, vector, or matrix

Symbolic input to be converted to MATLAB Function block with N outputs, specified as several symbolic expressions, functions, vectors, or matrices, separated by comma.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example:

collapse all

Name of the function, specified as a character vector. By default, matlabFunction(block,…) uses block as the function name.

See Specify Function Name for Generated Function.

Flag preventing code optimization, specified as false or true.

By default, matlabFunctionBlock generates a file containing optimized code. Optimized means intermediate variables are automatically generated to simplify or speed up the code. MATLAB generates intermediate variables as a lowercase letter t followed by an automatically generated number, for example t32.

See Disable Code Optimization.

Order of input variables and corresponding input ports of generated block, specified as a character vector, a vector of symbolic variables, or a one-dimensional cell array of character vectors, symbolic variables, or vectors of symbolic variables.

The number of specified input ports must equal or exceed the number of free variables in f. Do not use the same names for the input ports specified by Vars and the output ports specified by Outputs.

By default, when you convert symbolic expressions, the order is alphabetical. When you convert symbolic functions, their input arguments appear in front of other variables, and all other variables are sorted alphabetically.

See Specify Input Ports for Generated Block.

Names of output ports, specified as a one-dimensional cell array of character vectors. If you do not specify the output port names, matlabFunctionBlock uses names that consist of the word out followed by output port numbers, for example, out3.

Do not use the same names for the input ports specified by Vars and the output ports specified by Outputs. See Specify Output Ports.

More About

collapse all

Tips

  • To convert a MuPAD expression or function to a MATLAB symbolic expression, use f = evalin(symengine,'MuPAD_Expression') or f = feval(symengine,'MuPAD_Function',x1,...,xn). Then you can convert the resulting symbolic expression to a MATLAB Function block. matlabFunctionBlock cannot correctly convert some MuPAD expressions to a block. 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 conversion results. To verify the results, you can run the simulation containing the resulting block.

Introduced in R2009a

Was this topic helpful?