Using MATLAB® Functions and Data in Actions

MATLAB® Functions and Stateflow® Code Generation

You can call MATLAB® functions and access MATLAB workspace variables in Stateflow® actions, using the ml namespace operator or the ml function.

ml Namespace Operator

The ml namespace operator uses standard dot (.) notation to reference MATLAB variables and functions in action language. For example, the statement a = ml.x returns the value of the MATLAB workspace variable x to the Stateflow data a.

For functions, the syntax is as follows:

[return_val1, return_val2,...] = ml.matfunc(arg1, arg2,...)

For example, the statement [a, b, c] = ml.matfunc(x, y) passes the return values from the MATLAB function matfunc to the Stateflow data a, b, and c.

If the MATLAB function you call does not require arguments, you must still include the parentheses, as shown in the preceding examples. If you omit the parentheses, Stateflow software interprets the function name as a workspace variable, which, when not found, generates a run-time error during simulation.

Examples

In these examples, x, y, and z are workspace variables and d1 and d2 are Stateflow data:

ml Function

You can use the ml function to specify calls to MATLAB functions through a string expression in the action language. The format for the ml function call uses this notation:

ml(evalString, arg1, arg2,...);

evalString is a string expression that is evaluated in the MATLAB workspace. It contains a MATLAB command (or a set of commands, each separated by a semicolon) to execute along with format specifiers (%g, %f, %d, etc.) that provide formatted substitution of the other arguments (arg1, arg2, etc.) into evalString.

The format specifiers used in ml functions are the same as those used in the C functions printf and sprintf. The ml function call is equivalent to calling the MATLAB eval function with the ml namespace operator if the arguments arg1,arg2,... are restricted to scalars or string literals in the following command:

ml.eval(ml.sprintf(evalString, arg1, arg2,...))

Stateflow software assumes scalar return values from ml namespace operator and ml function calls when they are used as arguments in this context. See Inferring Return Size for ml Expressions.

Examples

In these examples, x is a MATLAB workspace variable, and d1 and d2 are Stateflow data:

ml Expressions

You can mix ml namespace operator and ml function expressions along with Stateflow data in larger expressions. The following example squares the sine and cosine of an angle in workspace variable X and adds them:

ml.power(ml.sin(ml.X),2) + ml('power(cos(X),2)')

The first operand uses the ml namespace operator to call the sin function. Its argument is ml.X, since X is in the MATLAB workspace. The second operand uses the ml function. Because X is in the workspace, it is included in the evalString expression as X. The squaring of each operand is performed with the MATLAB power function, which takes two arguments: the value to square, and the power value, 2.

Expressions using the ml namespace operator and the ml function can be used as arguments for ml namespace operator and ml function expressions. The following example nests ml expressions at three different levels:

a = ml.power(ml.sin(ml.X + ml('cos(Y)')),2)

In composing your ml expressions, follow the levels of precedence set out in Binary and Bitwise Operations. To repeat a warning note in that section, be sure to use parentheses around power expressions with the ^ operator when you use them in conjunction with other arithmetic operators.

Stateflow software checks expressions for data size mismatches in your action language during parsing of your charts and during run-time. Because the return values for ml expressions are not known until run-time, Stateflow software must infer the size of their return values. See Inferring Return Size for ml Expressions.

Which ml Should I Use?

In most cases, the notation of the ml namespace operator is more straightforward. However, using the ml function call does offer a few advantages:

ml Data Type

Stateflow data of type ml is typed internally with the MATLAB type mxArray. You can assign (store) any type of data available in the Stateflow hierarchy to a data of type ml. These types include any data type defined in the Stateflow hierarchy or returned from the MATLAB workspace with the ml namespace operator or ml function.

Rules for Using ml Data Type

These rules apply to Stateflow data of type ml:

Place Holder for Workspace Data

Both the ml namespace operator and the ml function can access data directly in the MATLAB workspace and return it to a Stateflow chart. However, maintaining data in the MATLAB workspace can present Stateflow users with conflicts with other data already resident in the workspace. Consequently, with the ml data type, you can maintain ml data in a Stateflow chart and use it for MATLAB computations in Stateflow action language.

As an example, in the following Stateflow action language statements, mldata1 and mldata2 are Stateflow data of type ml:

mldata1 = ml.rand(3);
mldata2 = ml.transpose(mldata1);

In the first line of this example, mldata1 receives the return value of the MATLAB function rand, which, in this case, returns a 3-by-3 array of random numbers. Note that mldata1 is not specified as an array or sized in any way. It can receive any MATLAB workspace data or the return of any MATLAB function because it is defined as a Stateflow data of type ml.

In the second line of the example, mldata2, also of Stateflow data type ml, receives the transpose matrix of the matrix in mldata1. It is assigned the return value of the MATLAB function transpose in which mldata1 is the argument.

Note the differences in notation if the preceding example were to use MATLAB workspace data (wsdata1 and wsdata2) instead of Stateflow ml data to hold the generated matrices:

ml.wsdata1 = ml.rand(3);
ml.wsdata2 = ml.transpose(ml.wsdata1);

In this case, each workspace data must be accessed through the ml namespace operator.

Inferring Return Size for ml Expressions

Stateflow expressions using the ml namespace operator and the ml function are evaluated in the MATLAB workspace at run-time. This means that the actual size of the data returned from the following expression types is known only at run-time:

When any of these expressions is used in action language, Stateflow code generation must create temporary Stateflow data, invisible to the user, to hold their intermediate returns for evaluation of the full expression of which they are a part. Because the size of these return values is not known until run-time, Stateflow software must employ context rules to infer their size for the creation of the temporary data.

During run-time, if the actual returned value from one of these commands differs from the inferred size of the temporary variable chosen to store it, a size mismatch error appears. To prevent these run-time errors, use these guidelines to write action language statements with MATLAB commands or ml data:

  1. The return sizes of MATLAB commands or data in an expression must match the return sizes of peer expressions.

    For example, in the expression ml.func() * (x + ml.y), if x is a 3-by-2 matrix, then ml.func() and ml.y are also assumed to evaluate to 3-by-2 matrices. If either returns a value of different size (other than a scalar), an error results during run-time.

  2. Expressions that return a scalar never produce an error.

    You can combine matrices and scalars in larger expressions because MATLAB commands practice scalar expansion. For example, in the larger expression ml.x + y, if y is a 3-by-2 matrix and ml.x returns a scalar, the resulting value is determined by adding the scalar value of ml.x to every member of y to produce a matrix with the size of y, that is, a 3-by-2. The same rule applies to subtraction (-), multiplication (*), division (/), and any other binary operations.

  3. MATLAB commands or Stateflow data of type ml can be members of the following independent levels of expression, for which the return size must be resolved:

  4. The return size for an indexed array element access must be a scalar.

    For example, the expression x[1][1], where x is a 3-by-2 array, must evaluate to a scalar.

  5. MATLAB command or data elements used in an expression for the input argument for a MATLAB function called through the ml namespace operator are resolved for size using the rule for peer expressions (preceding rule 1) for the expression itself, because there is no size definition prototype available.

    For example, in the function call ml.func(x + ml.y), if x is a 3-by-2 array, ml.y must return a 3-by-2 array or a scalar.

  6. MATLAB command or data elements used for the input argument for a graphical function in an expression are resolved for size by the function's prototype.

    For example, if the graphical function gfunc has the prototype gfunc(arg1), where arg1 is a 2-by-3 Stateflow data array, then the calling expression, gfunc(ml.y + x), requires that both ml.y and x evaluate to 2-by-3 arrays (or scalars) during run-time.

  7. ml function calls can take only scalar or string literal arguments. Any MATLAB command or data used to specify an argument for the ml function must return a scalar value.

  8. In an assignment, the size of the right-hand expression must match the size of the left-hand expression, with one exception: if the left-hand expression is a single MATLAB variable such as ml.x or a single Stateflow data of type ml, then the sizes of both left-hand expression and right-hand expression are determined by the right-hand expression.

    For example, in the expression s = ml.func(x), where x is a 3-by-2 matrix and s is scalar Stateflow data, ml.func(x) must return a scalar to match the left-hand expression, s. However, in the expression ml.y = x + s, where x is a 3-by-2 data array and s is scalar, the left-hand expression, workspace variable y, is assigned the size of a 3-by-2 array to match the size of the right-hand expression, x+s, a 3-by-2 array.

  9. In an assignment, Stateflow column vectors on the left-hand side are compatible with MATLAB row or column vectors of the same size on the right-hand side.

    A matrix you define with a row dimension of 1 is considered a row vector. A matrix you define with one dimension or with a column dimension of 1 is considered a column vector. For example, in the expression s = ml.func(), where ml.func() returns a 1-by-3 matrix, if s is a vector of size 3, the assignment is valid.

  10. If you cannot resolve the return size of MATLAB command or data elements in a larger expression by any of the preceding rules, they are assumed to return scalar values.

    For example, in the expression ml.x = ml.y + ml.z, none of the preceding rules can be used to infer a common size among ml.x, ml.y, and ml.z. In this case, both ml.y and ml.z are assumed to return scalar values. And even if ml.y and ml.z return matching sizes at run-time, if they return nonscalar values, a size mismatch error results.

  11. The preceding rules for resolving the size of member MATLAB commands or Stateflow data of type ml in a larger expression apply only to cases in which numeric values are expected for that member. For nonnumeric returns, a run-time error results.

    For example, the expression x + ml.str, where ml.str is a string workspace variable, produces a run-time error stating that ml.str is not a numeric type.

  12. There are special cases in which no size checking is done to resolve the size of MATLAB command or data expressions that are members of larger expressions. In the cases shown, use of a singular MATLAB element such as ml.var, ml.func(), ml(evalString, arg1, arg2,...), Stateflow data of type ml, or a graphical function returning a Stateflow data of type ml, does not require that size checking be enforced at run-time. In these cases, assignment of a return to the left-hand side of an assignment statement or to a function argument is made without consideration for a size mismatch between the two:

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS