How does the (Embedded) MATLAB Function work in Simulink
12 views (last 30 days)
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!
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:
a) Look into System Objects
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.