Call a block exactly once with Simulink Profiler
3 views (last 30 days)
Show older comments
Hi all,
I would like to know how much time the execution of a MATLAB Function Block in Simulink requires. For this, I have separated the MATLAB Function Block from the rest of my model and am using Simulink Profiler to assess the total time. According to my understanding, the time Simulink needs to execute the block once equals the total time given in the Profiler for one call of the block.
Yet no matter how I set up the model with the Function Block, the block always gets called more than once. I have included a picture of my current setup. I don't see any reason why the block should be called more than once. Placing the block in a triggered subsystem also results in the block getting called more than once.
Is there a way to ensure the block is only executed / called once? If not, can I simply divide the total time given by the Simulink Profiler by the number of calls to get the time of one call? Or am I misunderstanding something about the meaning of the number fo calls?
In case it is relevant, my application is the QP solver for a Model Predictive Controller. I do not yet have access to the physical hardware on which the controller will be implemented and am thus trying to roughly gauge the impact of different horizon lengths on the execution time of the solver in Simulink. I am also interested in how much execution times vary (hence my interest in only calling the block once).
Any help is greatly appreciated. Below is the mentioned picture.

0 Comments
Answers (1)
Fangjun Jiang
on 23 Jul 2025
"simply divide the total time given by the Simulink Profiler by the number of calls to get the time of one call"
That is a good estimate, knowing tht sometimes the execution time could vary based on inputs (more or less calculation depending on input value).
If you want to force the block to be executed only once, you could place it inside a "Initialize Function" or "Terminate Function" block (inside User-Defined Functions library). Make sure you have the proper input to activate the full execution of your code.
2 Comments
Fangjun Jiang
on 23 Jul 2025
It is interesting! I made a simple example. Use a persistent variable inside the MATLAB Function block to count how many times it is called. The output shows 51 which is the correst simulation steps for a default Simulink model. However, the Simulink Profiler shows it is called 113 times.
Anyone could provide a reasonable explaination?

See Also
Categories
Find more on Large-Scale Modeling 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!