Use Model Name as Programmatic Interface - MATLAB & Simulink (original) (raw)

You can use the name of a model as a programmatic interface for executing a specified simulation phase and computing values for individual states and times that you specify.

This functionality is not intended for running a model step by step, simulating a model programmatically, or for debugging. For those purposes, consider these alternatives:

Model Requirements

When you use the model name as a programmatic interface to compute simulation values, the software ignores the effects of state transitions and conditional execution. Use this functionality only for models that implement simple dynamic systems. Such systems must meet these requirements:

Using this functionality for models that do not meet these requirements and models that use multitasking execution can produce unexpected results. For more information about multitasking and single task execution, see Time-Based Scheduling and Code Generation (Simulink Coder).

Input Arguments

To use a model name as a programmatic interface, you use the name of your model as though it were the name of a function. You always provide four inputs:

The number, type, and dimensions of the output arguments depend on which simulation phase you execute.

When you use this functionality, you must manually execute each simulation phase in the appropriate order. For more information about how simulations run, see Simulation Phases in Dynamic Systems. This functionality is not meant to run a simulation step by step or as a replacement for the typical simulation workflow.

Execute Size Computation Phase

To run the sizes phase, use this syntax, specifying [] for the first three input arguments.

[sys,x0,blks,st] = modelName([],[],[],"sizes");

Before R2024a: You can also execute the sizes phase without specifying any input arguments.

In R2024a: The syntax with no input arguments is no longer supported.

The sizes phase returns four output arguments:

Execute Compilation Phase

To run the compilation phase, use this syntax, specifying [] for the first three input arguments.

[sys,x0,blks,st] = modelName([],[],[],"compile");

The compilation phase returns the same four output arguments as the sizes phase:

After running the compilation phase, you must run the termination phase before you can close the model. If you execute the compilation phase multiple times before executing the termination phase, you must execute the termination phase an equal number of times.

Compute Discrete State Values

To execute the update phase and compute the discrete state values, use this syntax. You specify the time at which you want to calculate the discrete states and the current state and input values to use in the computation.

dStates = modelName(t,x,u,"update");

The update phase returns the discrete state values dStates as a structure or an array, depending on how you specify the current state values,x.

Compute Output Values

To compute the model outputs, use this syntax. You specify the time at which you want to calculate the discrete states and the current state and input values to use in the computation.

out = modelName(t,x,u,"outputs");

Compute Continuous State Derivatives

To compute the derivatives for continuous states, use this syntax. You specify the time at which you want to calculate the discrete states and the current state and input values to use in the computation.

derivs = modelName(t,x,u,"derivs");

Execute Termination Phase

When you are done analyzing the model behavior, use this syntax to execute the termination phase so you can close the model. Specify [] for the first three input arguments.

modelName([],[],[],"term");

See Also

sim | parsim | batchsim

Topics