How do I incorporate MATLAB code into Simulink?
29 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
MathWorks Support Team
il 22 Lug 2016
Modificato: MathWorks Support Team
il 31 Gen 2025
How do I incorporate MATLAB code into Simulink? I would like to put my existing MATLAB algorithm into a Simulink block.
Risposta accettata
MathWorks Support Team
il 17 Gen 2025
Modificato: MathWorks Support Team
il 31 Gen 2025
There are 4 ways to incorporate MATLAB code into a block in your Simulink model:
1. MATLAB Function block: https://www.mathworks.com/help/releases/R2021a/simulink/slref/matlabfunction.html
2. Interpreted MATLAB Function block: https://www.mathworks.com/help/releases/R2021a/simulink/slref/interpretedmatlabfunction.html
3. Level-2 MATLAB S-Function: https://www.mathworks.com/help/releases/R2021a/simulink/sfg/writing-level-2-matlab-s-functions.html
4. MATLAB System block: https://www.mathworks.com/help/releases/R2021a/simulink/ug/what-is-matlab-system-block.html
Which of the 4 ways to choose depends mainly on a tradeoff between ease of use and performance. That being said, requiring a certain feature (like the use of buses or code generation) may reduce the number of available options. The important features to consider are noted at the following link:
It is good to begin with the MATLAB Function block as shown below because it may be significantly easier to transition your code to the MATLAB Function block than the Level-2 MATLAB S-Function or MATLAB System block. However, if you are an advanced user and you know from the beginning that you will be using your MATLAB code in both MATLAB and Simulink, or are open to the possibility of making significant changes to your existing code, then the MATLAB System block and Level-2 MATLAB S-Function block should be considered as they have the most flexibility and features.
Recommended workflow for incorporating your custom MATLAB function in Simulink:
(1) Use the MATLAB Function block to call your function in Simulink.
Make sure your existing MATLAB function is on your MATLAB path. Then, add a MATLAB Function block to your model with the same inputs and outputs as your existing MATLAB function, and then simply call your function from inside the MATLAB Function block.
If you get an error stating that a function is not supported for code generation, you can use coder.extrinsic on your function. Note that if you do so, the MATLAB Function block may be slow because, like the Interpreted MATLAB Function block, it is making calls to MATLAB to execute coder.extrinsic functions. That being said, it is easy to implement in this way and creates a block with an input and output for each of the inputs and outputs of your original function.
Information about the MATLAB Function block and coder.extrinsic can be found at the links below:
(2) If the performance is too slow and the MATLAB Function block has all necessary features, take your original function out of coder.extrinsic and then go into your original function and put coder.extrinsic for all functions throwing errors because they are not supported for code generation.
Alternatively, if you have the MATLAB Coder toolbox, you can use the Code Generation Readiness Tool to graphically screen your code for MATLAB functions and features which are not supported for code generation and then use that information to help modify your code:
(3) Look at functions being declared coder.extrinsic or noted by the Code Generation Readiness Tool as not compatible with code generation. Modify the code using those functions to be compatible with code generation. This can be done by substituting in functions and features which are supported for code generation. Once the functions are code compatible, remove the corresponding coder.extrinsic lines. Functions and features supported for code generation can be found at the links below:
By Alphabet: https://www.mathworks.com/help/releases/R2021a/referencelist.html?type=function&capability=codegen&listtype=alpha
By Category: https://www.mathworks.com/help/releases/R2021a/referencelist.html?type=function&capability=codegen
MATLAB Language Features: https://www.mathworks.com/help/releases/R2021a/simulink/ug/matlab-language-features-supported-for-code-generation.html
For System Objects: https://www.mathworks.com/help/releases/R2021a/simulink/ug/code-generation-for-system-objects.html
To find more information on flow chart for how to decide along with comparisons among all blocks that enable the use of MATLAB code in Simulink in MATLAB R2019b, execute the following command in the command window to view the release-specific documentation:
>> web(fullfile(docroot, 'simulink/ug/comparison-of-custom-block-functionality.html'))
Please refer to the link below to find the required information about the latest release. This is particularly important for step (3), as the list of functions and features supported for code generation is updated with each release.
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Simulink Coder in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!