MATLAB: In simulink, how to call functions (such as “quadv”) which need to call function handles

embedded functionfunction handlematlab coders-function

Kaustubha Govind answered another similar question earlier kindly. She mentioned: "Function handles were not supported in Embedded MATLAB Function blocks until fairly recently, additionally, fsolve is not amongst the functions supported for code-generation, which are the only functions that can be used directly in a Embedded MATLAB Function block."
Also she suggested to use "interpreted matlab funcion". This might be the easiest way to do so. Thanks for Kaustubha Govind's answer firstly.
Yet I think interpreted matlab function might cause the simulation slow.
I am wondering if there is other ways to solve this problem?
Hope people with the same experience could discuss here.

Best Answer

First, regarding QUADV in particular, the MATLAB Function Block supports QUADGK. You may find that it is competetive to call QUADGK in a loop on each component. Second, you can call MATLAB functions "extrinsically" from a MATLAB Function Block. However, you cannot pass a function handle from a MATLAB Function Block to an extrinsic function. The workaround is to define your own MATLAB function (something you will not compile) that accepts all the data (parameters) it needs to call FSOLVE or whatever and returns data to your MATLAB Function Block code. That will result in some interpreted code, whatever is in the MATLAB function that you declare extrinsic and call from your MATLAB Function Block, but the amount of interpreted code will be minimized.
I will add, however, that much of core MATLAB is compiled and optimized, so just because MATLAB is an "interpreted" language doesn't mean that it will be slower. It depends on what the code is doing, how it is written, and where it spends most of its time.