Get current simulation step (or time) in Simulink

108 views (last 30 days)
Hello,
in my Simulink simulation, I need to access certain elements in an array, the indices depending on the current simulation step.
I found a solution that works, but takes some possibly avoidable simulation time. Because I need to conduct many simulations I would like to know if there are more efficient solutions.
Just to maybe better understand what I want to do, my current solution:
I calculate the current simulation step using the clock Simulink-block as in this image:
Then I use this as an input to some selector blocks to get the correct entries from an array.
I already checked, what needs much time is the part in the picture. Not the selector-blocks.
So my problem would be solved if there was an option to get the current simulation step (or alternatively, the current simulation time, for I use fixed step size) directly in Simulink, without using a clock. Maybe it is available somewhere as a variable/a parameter?
I am also open to completely different solutions.
I would be thankful for any help!
Best Regards
Sofie

Answers (1)

Jim Riggs
Jim Riggs on 24 May 2023
It looks like what you want is a simple counter. Try this:
You don't need to use a time generator and a multiply (and floor). The above will provide a simple integer count using only a single add operation. This will increment by one every pass through the simulation.
Note that on the first pass the "Memory" block will have a default value of zero, therefore "Count" will be equal to 1 from the start. You can change this by changing the initial value in the memory block if you want to start with a different value. For example, if you set the initial value of the memory block to -1, "Count" will start from zero (1 + (-1) ).
  2 Comments
Sofie Brammer
Sofie Brammer on 30 May 2023
Edited: Sofie Brammer on 30 May 2023
Thank you very much for your answer! :)
This at least is an easier-to-understand version and therefor better compared to my solution, but sadly it does not speed-up the simulation.
What would be perfect is if it would be possible to just get the current simulation step or time as a variable / parameter to write something like
array(:, current_step)
into a constant-block. If something like this works.
Or maybe the increased simulation time is anyways somehow caused by the fact that I use different values each time, so there might be more restoring involved? I am not sure how exactly the data is treaten in Simulink.
All I observed is, if I replace the blocks I posted by a constant block, the simulation is faster.
If I replace it by what you posted, it is the same than with my version.
When I use a random integer as the input to the selector, other effects due to my simulation purpose apply that make the simulation slow, so I can not compare like this.)
So maybe not these blocks themselves are what makes a difference anyways. Maybe it is just the fact that I have a constant input into the selector-block and all the time use the same data from my arrays.
In that case, it would not matter how exactly I select the data from the array.
Karim Darwich
Karim Darwich on 14 May 2024
@Jim Riggs Good morning sir
Is there an other way that we can access step time without using a memory bloc ?

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!