How can I use a probability distribution object in a Stateflow chart?

6 views (last 30 days)
Hi guys,
I'm trying to use a probability distribution object (from the function U = makedist('exp',...)) to model stochastic transitions between states in Stateflow, but I can't get it to work. In my chart I'm using the temporal logic after(u,sec), for which I want the argument u to be a number generated by the probability distribution object, U. I've tried to use u = random(U) but after(u,sec) gives me an error complaining about:
"Cannot perform an implicit cast between these two classes.
Transition 'after(u,sec)'
"u" "
Even though u is declared as a double in my chart and the function after() takes a double as first argument.
Does anyone have any experience from a similar problem or know if it's possible? The goal with my little project is to be able to model situations where different transitions between states may take different times according to different distributions.
  1 Comment
Olivier NEYRET
Olivier NEYRET on 13 Jul 2021
Hello, did you find an answer to your problem, I have the same and I would like to know how to do it

Sign in to comment.

Answers (1)

Andreas Apostolatos
Andreas Apostolatos on 28 Sep 2021
Firstly please note that Simulink is under the hood generating and compiling code when you build and run a model. The Coder products do not support Code Generation for every MATLAB function. In particular, function 'makedist()' is not supported for Code Generation, otherwise this should be explicitly mentioned in the corresponding documentation page under the Section "Extended Capabilities", see the following documentation page accordingly,
If you do not need to generate standalone C/C++ code from your model, then you can use identifier 'persistent' for the probability distribution object in your state, so that the variable persists when the state goes out of scope and it is not recreated after entering the state again, and subsequently declare function 'makedist()' as 'coder.extrinsic()', such that Simulink does not generate code from this function, see also the following documentation pages accordingly,
Thus, the MATLAB code inside your Stateflow state could looks as follows,
U = makedist('exp');
u = zeros(1, 1);
u = random(U);
Please note that I define variable 'u' to hold a zero value, before assigning to it the random number using function 'random()'. The reason is that Simulink does not know which type it should assign to this variable when compiling the MATLAB code, and in this way you can make sure that Simulink knows its type.
Afterwards you should be able to use variable 'u' within function 'after()' on a state transition, such as,
I hope that this information is useful for your application.
Kind regards,


Find more on Execution in MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!