Azzera filtri
Azzera filtri

How does the (Embedded) MATLAB Function work in Simulink

14 visualizzazioni (ultimi 30 giorni)
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:
  1. 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 / ... )
  2. Is the Simulink Coder used when simulating a Model containing a MATLAB-Function? (except if you build the Model for an external target)
  3. Does the System target have any influence on the simulation speed?
  4. 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!

Risposte (2)

Ryan G
Ryan G il 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.
  3 Commenti
Ryan G
Ryan G il 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 il 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):

Accedi per commentare.

Philipp il 5 Mar 2014
Answer to Question 1: I made a simple test to check this issue: I changed something and looked at the "Date modified" in the Windows Explorer to see if it changed.
First I inserted some Blocks to the Model and simulated it. The Date modified didn't change.
Then I changed some Parameters (one in the Model and one in the MATLAB Function) and simulated it. The Date modified didn't change.
Then I added an useless command in the MATLAB Function and simulated it. The Date modified changed
--> It is generated, when something in the MATLAB Function was changed.
Note: The Date modified of the source-files (s-function, ...) is always the same as the mex-file.
  1 Commento
Philipp il 5 Mar 2014
The Parameter of the MATLAB Function I changed was one it gets from the Workspace of Matlab, so no piece of code in the MATLAB Function was changed.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by