How does the (Embedded) MATLAB Function work in Simulink
8 views (last 30 days)
Show older comments
I'm very interested in understanding how (Embedded) MATLAB Functions in Simulink work. I spent a lot of time searching for a satisfying answer, but I didn't find it yet.
What I know about the MATLAB Funciton:
- It's a possibility to integrate MATLAB Code in a Simulink Model
- It's possible to build a Model containing a MATLAB Funciton for an external target
- It generates C-Code from the Matlab Code
- It stores a mex-File of the Model in the working directory and the generated C-Code as a S-Function in "working directore\slprj\_sfprj\dInt_embedded\_self\sfun\src
- In the Simulink Profiler you can see, that it takes some time simulating the MATLAB Function besides running the generated S-Function
now some concrete questions:
- When is the C-Code and the mex-Function being generated? ( Each time the Model was Changed (and then run) / Each time the MATLAB Funciton was changed / Each time some Parameters in the Model were changed / ... )
- Is the Simulink Coder used when simulating a Model containing a MATLAB-Function? (except if you build the Model for an external target)
- Does the System target have any influence on the simulation speed?
- What are the Settings for the fastest possible Simulation of a Model containing a MATLAB Function? ( Normal Mode / Acceleration Mode / first building the Model, then simulating it / run the Code Generation Advisor to optimize the Model for Execution efficiency / ... )
I would be glad if at least some of the questions could be answered!
Many thanks in advance!
0 Comments
Answers (2)
Ryan G
on 5 Mar 2014
Thanks for the well written questions.
1) The function regenerates anytime you change the MATLAB function or parameters within the MATLAB function. If you change parameters that are (1) inputs or (2) not part of the MATLAB function, it shouldn't have to re-code.
2) As far as I know (complex product line, tough to know everything) Simulink Coder is not needed to use MATLAB-Function blocks for simulation
3) For simulation I would imagine it won't matter. Since it has to build the mex-file I would assume the target is only for code-gen.
4) Complicated question. When you run Accelerator or Rapid Accelerator mode there is overhead for generating and compiling code. If you're making changes it has to do this every time. That overhead may make it faster to run in normal mode. If you want to improve your model right now, I would suggest using the Performance Advisor.
A few things:
b) I personally avoid using MATLAB Function blocks in Simulink if I can avoid them. Obviously sometimes it's the best answer. Evaluate the necessity of the blocks. What are you using them for? Maybe Answers can recommend alternatives.
3 Comments
Ryan G
on 6 Mar 2014
If you want to vary parameters in an s-function you want to setup tunable parameters. If you right-click create s-function on a block, a dialog will popup giving you the opportunity to set those tunable parameters.
Did you try the Performance Advisor?
Ketan
on 9 Mar 2014
For normal mode simulation, disable debugging to improve MATLAB Function block performance:
Your use case (modify tunable parameters and re-run simulation) seems like it could benefit from rapid accelerator workflow. Check out the following example (if you do not have PCT use a regular for-loop instead of parfor and ignore the worker init/cleanup code):
See Also
Categories
Find more on Simulink Coder in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!