MATLAB source code contains a functions declaration. The m-file is a text file containing a list of commands written in MATLAB or Octave syntax. You can define functions and scripts in the m-file.
Creation of elements from MATLAB source code is supported in the following diagrams:
Wrapping the mathematical expressions from MATLAB source code
To wrap the mathematical expressions from MATLAB source code into the model, use one of the following methods:
- Drag the m-file directly to the diagram pane to create one of the following:
- Constraint Block with its properties and parameters in the SysML Block Definition or UML Class diagrams.
- Constraint Property and Constraint Block in the SysML Internal Block, SysML Parametric, or UML Composite Structure diagrams. Constraint Block is automatically set as the type for the newly created Constraint Property. The properties and parameters of the Constraint Block are displayed on the Constraint Property shape.
- Call Behavior Action with its pins and opaque in the SysML or UML Activity diagrams.
- Drag the m-file onto the already existing element shape (empty or full) to set one of the following:
- Constraints and parameters for Constraint Block in the SysML Block Definition or UML Class diagrams.
- Type for Constraint Property by creating a new Constraint Block in SysML Internal Block, SysML Parametric, or UML Composite Structure diagrams. The properties and parameters of the newly created Constraint Block are displayed on the Constraint Property shape.
- Use the shortcut menu on the already existing element shape (empty or full). Choose Tools > Create Expression from M-File.
- The name of the created element is the same as the name of the function in the m-file. If the name of that element already exists, it will appear with an incremental number at the end of the name.
- If the m-file contains multiple functions declarations, only the first function is wrapped into the model.
Setting the constraints and parameters for Constraint Block using m-file
To set constraints and parameters for a Constraint Block using m-file
- In the Containment tree or on the diagram pane, select a Constraint Block or Constraint Property (with set type).
- From the shortcut menu, choose Tools > Create Expression from M-File. The Open file dialog opens.
- Select the m-file and click Open.
The constraints and parameters are set for Constraint Block.
The following figure illustrates the function declaration in m-file. It is for least-square fitting to find the slope (m) and the y-intercept (b) of a straight-line equation from two given input parameters x and y. The constraint expression is: [m, b] = linefit(x, y). The constraint parameters are x, y, m, and b. After the function transfer into the element, its type, property, and parameter are set automatically and are displayed on the element shape.