Stateflow    

MATLAB Name Space Operator

The MATLAB name space operator, ml, is used to get and set variables in the MATLAB workspace. The ml operator can also be used to access MATLAB functions that operate on scalars in a convenient format.

Use the notation, a = ml.func_name();, to call a MATLAB function that does not accept any arguments. Omission of the empty brackets causes a search for a variable of the name specified. The variable will not be found and a runtime error is encountered during simulation.

Use of the ml name space operator should be avoided if you plan to build a Real-Time Workshop target that includes code from Stateflow Coder.

Example: Using the ml Operator to Access MATLAB Workspace Variables

This is an example of using the ml operator to get and set variables in the MATLAB workspace.

These data objects are defined in the Stateflow diagram:

The values of a and y are accessed in the MATLAB workspace and used in the expression with the Local data objects d1 and d2. The result of the expression is assigned to the MATLAB workspace variable x. If x does not exist, it is automatically created in the MATLAB workspace.

Example: Using the ml Operator to Access MATLAB Functions

This is an example of using the ml operator to access MATLAB functions.

These data objects are defined:

x, y, and z must be defined in the MATLAB workspace prior to starting the simulation; otherwise a runtime error is generated at the execution time of the transition.

A MATLAB function named my_func is called with these arguments:

  1. x(1,3)
  2. y(3)
  3. z
  4. d1
  5. d2
  6. string 'abcdefgh'

The result of my_func() (if it is a scalar) is assigned to element (5, 6, 7) of a multidimensional matrix v in the MATLAB workspace. If v does not exist prior to the execution of this statement, then it is automatically created by MATLAB workspace.

If my_func() returns a vector, the first element is assigned to v(5,6,7). If it is a structure, a cell array, or a string, the result is undefined.


 ml() Functions The ml() Function Versus ml Name Space Operator