How to call extrisic m-file contains function handle in SIMULINK user-defined MATLAB function?

2 vues (au cours des 30 derniers jours)
I am trying to understand how to call extrisic m-file contains function handle in SIMULINK user-defined MATLAB function. Here I wrote a test to call an extrisic m-file named "fun_kk" in my SIMULINK user-defined MATLAB function as below:
***********************
function y = actuator(u)
coder.extrinsic('fun_kk')
d = fun_kk(u);
y = d;
**************************
And the "fun_kk" is:
*****************
function d = fun_kk(u)
d = sqrt(@(u)u^2);
*****************
After runnung SIMULINK by giving a contant input u, the error message apears:
Function output 'y' cannot be an mxArray in this context.
Consider preinitializing the output variable with a known type.
Function 'MATLAB Function' (#159.9.10), line 1, column 10:
"y"
Launch diagnostic report.
Could anyone please help me to solve this problem?

Réponses (3)

Thomas Marullo
Thomas Marullo le 4 Déc 2015
Have you solved this problem? I am having the same problem.

Ryan Livingston
Ryan Livingston le 14 Fév 2017
Modifié(e) : Ryan Livingston le 14 Fév 2017
You can see the explanation of this here:
Namely, the code generator can't know the output type of the extrinsic function call. You can pre-assign the output to tell it what the type, size, and complexity are:
coder.extrinsic('fun_kk')
y = 1; % assuming real scalar double output for fun_kk
% change to match the expected type, size, complexity
y = fun_kk(u);
Now, fun_kk only uses constructs which are supported for codegen (anonymous functions are supported as of R2016b) so there's no need to use coder.extrinsic. Just call fun_kk, omit the coder.extrinsic, and everything should work out.

Fayez Alruwaili
Fayez Alruwaili le 29 Jan 2021
d = zeros (r,c)
d = fun_kk(u);
....

Catégories

En savoir plus sur Simulink Functions dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by