Stateflow | ![]() ![]() |
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:
ml.var
, ml.func()
, or ml(evalString, arg1, arg2,...)
, where var
is a MATLAB workspace variable and func
is a MATLAB function, cannot be known until 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 till run-time, Stateflow 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 results. To prevent these run-time errors, use the following guidelines in constructing action language statements with MATLAB commands or ml
data:
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.
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.
ml
can be members of the following independent levels of expression, for which the return size must be resolved:
The expression for each function argument is a larger expression for which the return size of MATLAB commands or Stateflow data of type ml
must be determined. For example, in the expression z + func(x + ml.y)
, the size of ml.y
has nothing to do with the size of z,
because ml.y
is used at the function argument level. However, the return size for func(x + ml.y)
must match the size of z
, because they are both at the same expression level.
The expression for an array index is an independent level of expression that is required to be scalar in size. For example, in the expression x + arr[y]
, the size of y
has nothing to do with the size of x
because y
and x
are at different levels of expression, and y
must be a scalar.
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.
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.
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.
Note
Currently, Stateflow supports only scalar and ml type graphical function arguments and return types.
|
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.
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.
s = ml.func(x)
, where x
is a 3-by-2 matrix and s
is scalar data in Stateflow, 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.
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.
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.
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.
Note
Member MATLAB commands or data of type ml in a larger expression are limited to numeric values (scalar or array) only if they participate in numeric expressions.
|
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:
For example, in the expression ml.x = ml.y
, ml.y
is a MATLAB workspace variable of any size and type (structure, cell array, string, and so on).
For example, in the expression ml.func(m_x, ml.x, gfunc())
, m_x
is a Stateflow data of type ml
, ml.x
is a MATLAB workspace variable of any size and type, and gfunc()
is a Stateflow graphical function that returns a Stateflow data of type ml
. Even though Stateflow does nothing to check the size of the input type, if the passed-in data is not of the expected type, an error results from the function call ml.func()
.
Note
If inputs in the preceding cases are replaced with non-MATLAB numeric Stateflow data, a conversion to a ml type is performed.
|
![]() | ml Data Type | Data and Event Arguments | ![]() |