Get current simulation step (or time) in Simulink

조회 수: 48 (최근 30일)
Sofie Brammer
Sofie Brammer 2023년 5월 24일
편집: Sofie Brammer 2023년 5월 30일
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

답변 (1개)

Jim Riggs
Jim Riggs 2023년 5월 24일
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) ).
  댓글 수: 1
Sofie Brammer
Sofie Brammer 2023년 5월 30일
편집: Sofie Brammer 2023년 5월 30일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by