Why is the code replacement triggered for the Matrix Inverse block but not the inv() function in a MATLAB function block?

1 view (last 30 days)
I am generating code for a model with a MATLAB Function block. In the MATLAB Function block, I call the inv() function.
I expect the code generated for the inv() function by embedded coder to be replaced by the function "matrix_inv_2x2_double" (a function specified in the rtwdemo_crlmatops demo). However this does not happen. On the other hand, the matrix inverse block is indeed replaced by this function. 
Is there a way to get the inv() function in the MATLAB Function block replaced by "matrix_inv_2x2_double"?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 18 Jan 2024
Edited: MathWorks Support Team on 29 Feb 2024
Unfortunately, "inv" cannot be replaced when used in a MATLAB Function block, since this is a limitation with replacing functions in MATLAB code: 
  1. Please run the below command in the MATLAB R2018b command window to get the release specific documentation
    web(fullfile(docroot, 'ecoder/ug/code-you-can-replace-from-simulink-models.html'))
  2. Please run the below command in the MATLAB R2018b command window to get the release specific documentation
    web(fullfile(docroot, 'ecoder/ug/code-you-can-replace-from-matlab-code.html'))
The former contains "inv" as an operator that can be replaced, but the latter does not. Thus, the Code Replacement Library does not support the inv() function. There is a workaround to this however: the "inv" operation was wrapped inside an additional MATLAB Function "myInvWrapper". A code replacement for that was then specified, using the following workflow:
Please run the below command in the MATLAB R2018b command window to get the release specific documentation
web(fullfile(docroot, 'ecoder/ug/replace-matlab-functions-specified-in-matlabfunction-blocks-sc.html'))
Please follow the below link to search for the required information regarding the current release:

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!