Should I use a MATLAB Function block or normal Simulink blocks?

2.8k views Asked by At

I have read that using an Matlab Function within a Simulink model can severely impact its performance. The recommendation is to use the MATLAB Function block instead of the Interpreted MATLAB Function block.

It's not clear to me how this applies when compiling the model into C for use on an xPC Target machine. Am I subject to some kind of penalty because of the M-script, or am I better off using normal Simulink blocks to solve the same problem?

2

There are 2 answers

0
Navan On BEST ANSWER

There should not be any penalty because of use of MATLAB Function block. When compiling the model MATLAB Function block compiles its MATLAB code into C like other Simulink blocks.

I would pick based on the best way to model the problem. For example, if your algorithm needs too many control flow statements or loops it is easier to code it in MATLAB Function block. You can always do a mixture of both.

0
Phil Goddard On

The link you give doesn't really say or imply that "using an Matlab Function within a Simulink model can severely impact its performance". It can in some instances, but in many cases it is just as fast as build-in blocks.

The Interpreted MATLAB Function block calls back into MATLAB every time it needs evaluating (i.e. at every time step). That makes it slow (relative to other techniques) and also prevents it being used for any c-code generation purposes (i.e Simulink Coder, which is used by xPC).

m-code in a MATLAB Function block that uses functions defined as coder.extrinsic suffer from the same limitations as above and can be slow.

m-code in a MATLAB Function block that is within the Embedded NATLAB subset (i.e simple math and matrix manipulation) gets converted to C-code and compiled (when using Simulation and when used in c-code generation). In simulation and code-gen you should see similar performance to using standard blocks.

Phil.