Simulink Counter counts random numbers (not by order)

2 views (last 30 days)
I've been working with the Counter block for the past week and at the beginning it all worked great - the counter seemed to count up, until a Maximum Count value I provided, and by the right order according to the sample time.
I'm pretty new to Simulink and I can't tell if I changed anything that might have affected it, but something happened and the Counter block seems to act up and randomly change the count value within the range I provide (considering Maximum Count) - meaning, instead of counting from 0 to 3 in the right order, it would just randomly change numbers within that range.
This is the same issue even when I set up my own counter, not using any counter blocks (I did it using a Constant, Add and Memory blocks).
Any idea what might have caused it and how to reverse it?

Answers (2)

Andy Bartlett
Andy Bartlett on 12 Oct 2022
Edited: Andy Bartlett on 12 Oct 2022
Check the sample times of the blocks being used.
One hunch is that your are running the blocks in continuous-time. And, the Scope or whatever you are using to view the blocks is only showing you a few samples. Like a strobe-light effect, the values look randomly jumpy, but they really are counting properly between the visible samples.
To easily inspect sample times, go to the left border on your Simulink model. Hover over the two parallel arrows, a tool tip should say Sample Times. Click the parallel arrow icon and put a check mark next to "All".
Then look of the "Timing Legend" to the right of the model. If that is not showing, either do Ctrl-J or Click the parallel arrow icon again and click the last row "Timing Legend".
The blocks involved with the Counter should all be set to operate at a some Discrete Sample Time.
  1 Comment
Shani on 13 Oct 2022
Thanks for the answer!
Unfortunaly, after having chekced that, all blocks are discrete. Is there any other way to approach this?

Sign in to comment.

Andy Bartlett
Andy Bartlett on 13 Oct 2022
If you have not already done so, try using the Simulink debugger.
Instead of clicking the Run icon on the toolstrip, click the Step Forward icon.
Then right-click on the key input and output signals and select
"Show Value Label of Selected Port"
to get little yellow boxes showing the current value of the signal.
For this portion of your model:
>> I set up my own counter, not using any counter blocks (I did it using a Constant, Add and Memory blocks).
stepping through the simulation is likely to be especially helpful because you can see all the details.
If you need to control input values, then consider using Repeated Sequence Stair Blocks. I find these very helpful in debugging, because they allow me to isolate things and quickly get to interesting sequences of inputs.
Hopefully, single stepping using the Simulink debugger will allow you to quickly find a point where the model's actual behavior does not agree with your mental model of how it should behave. Once you've isolated where the disagreement begins you'll likely be able to resolve things.
If you can't resolve it own your own, try to create reproductions steps that are as simple as possible. For example, remove everything from you model not necessary to reproducing the disagreement. Also seek to highly simplify any external dependencies like M files and MAT files that define parameters used by the reproduction model. The fewer external files and parameters the better. Then share the simplified reproduction model with others. For example, up load the highly simplified model and all necessary supporting M file, MAT files, non-MathWorks Simulink libraries to MATLAB Answers. Nothing slows down getting a helpful answer more than not providing all the needed items another person would require for reproduction.
When I upload reproduction steps, I find it very helpful to "dog-food" the steps. I download the reproduction items to a clean directory, check my MATLAB path just includes things from the MathWorks install and not specific to me or my group. Then I go through my own reproduction steps. Around 40% of the time my dog fooding shows that I forgot to upload something in my first and sometimes 2nd and 3rd attempts to provide usable reproduction steps. The dog fooding usually saves many days of frustration for me and the people seeking to utilizing my reproduction steps.
Hope the debugger solves your issue.


Find more on Simulink Functions 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!