MATLAB: How to get the minor time in a MATLAB s-function
s-functionsimulink
Inside a Level-2 Matlab S-function, block.CurrentTime gives the value of the major time. How can we get the value of the minor time?
Best Answer
Actually, block.CurrentTime gives the value of the current time-step in the simulation (could be either major or minor). The block outputs and derivatives (and potentially, zero-crossings) methods are run in minor time-steps. So if you are querying block.CurrentTime in one of these functions, you should simply have to check block.IsMajorTimeStep to verify if you're in a major/minor time-step.
An inherited and fixed-in-minor-step sample time is invalid for Level-2 MATLAB S-Functions. For a list of valid sample time, offset pairs please refer to the documentation under the following link:
To workaround this issue you can implement the IsMajorTimeStep() method in the Output() or Update() methods of the Level-2 MATLAB S-Function to determine whether the current simulation time step is a major time step.
Minor time steps occur if your model has continuous states. It occurs during the integration stage of your model. Simulink uses a numerical integration algorithm such as ODE45 to simulate a system. During the integration stage the solver obtains the derivatives from the system and uses it to compute a new state vector for the next time step. This entire process is called the integration phase or minor time step.
Page 3-16 of the Writing S-functions manual (version 3.0) shows a graphical representation of the calling sequence of an S-function which should help to make this more clear. This is also shown here:
Best Answer