Guy on Simulink

Simulink & Model-Based Design

Creating Your Own Block With Continuous States (MATLAB S-Function) 5

Posted by Guy Rouleau,

In a previous post, I showed an example of a MATLAB S-Function with a discrete state

This week, we continue in the same direction and implement an S-Function with a continuous state.

Continuous States

To begin, let's go back to the question posted on MATLAB Answers by K E:

What are continuous and discrete states in Simulink?

and see what Kaustubha Govind mentionned about continuous states:

With continuous states however, Simulink asks the block to provide a derivative (dx/dt) of the state in the Derivatives() method and uses its ODE solver to compute the integral of dx/dt to obtain 'x'. This 'x' can then be accessed in the Outputs() function. For example, to implement an Integrator block...

Let's stop here and see how to implement an Integrator block step by step.


In an S-Function, the continuous states are declared in the setup method:

Declaring continuous state

Continuous states can not be hidden, like discrete states. There are many reason for this, for example, they are used by the Simulink solver to compute steps taken by the variable time-step solver.

For details on the DirectFeedthrough flag, look at the previous post about discrete states.

Initialize Conditions

Like the discrete state, you need to initialize continuous states:

Initialization of continuous state


In the Outputs method, you can read the value of the continuous state and assign it to the output port:

Output of method using continuous state


Here is the most important part. When you have continuous states, you are not supposed to write to block.ContStates.Data, except for initialization. Instead, you specify the derivative of the states and write that to block.Derivatives.Data. For an Integrator block, the input is the derivative of the output:

Update of method using continuous state

The next time the output method will be called, the solver will have integrated the value specified in block.Derivatives.Data and stored the result in block.ContStates.Data.


The important thing to realize about continuous states is that the block provides the derivative of the states to the solver. The solver takes care of the integration and gives back to the block the result of the integration.

For an example like that, look at the demo model msfcndemo_limintm.mdl

Now it's your turn

Share your S-Function experiences by leaving a comment here.

5 CommentsOldest to Newest

Ashish Sadanandan replied on : 1 of 5

Do you really have a lot of users that use M-file S-Functions? IMHO, these articles would be a lot more interesting to read (and helpful) if you started writing about C S-Functions instead.

Guy Rouleau replied on : 2 of 5

@Ashish Sadanandan: Thank you for the comment. My plan is definitely to follow up with C S-Functions.

I decided to highlight a few basic concepts using MATLAB S-Function because, in my opinion, it is easier to understand and debug. But I agree with you that C S-Functions offer a lot more possibility and will be useful for more users.

usdb replied on : 3 of 5

i used sfunction (m-code)in my programm(extended kalman filter),i have not a good result;ihave 5 discrets states and plus the states equations we have error covariance Pe to calculate (reccurant equation) every itteration (in flag 2):when i used the debug i see that Pe calculated is not used in the after itteration but Pe initial.
i writed Pe initial before flag 0
thank you.

upul replied on : 4 of 5

How can I build an S function for more than one output and more than one input. As an example if it is required to compare three values (a, b, c) and if a=b then out1 shall be 1.If b=c then out2 shall be 1. otherwise outputs shall be 0. How to write an s-function for something like that.

AJ replied on : 5 of 5

This article really helped! Defo big thanks to you for this insightful information. Keep up the good work!

Add A Comment

Your email address will not be published. Required fields are marked *


Preview: hide