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

1 view (last 30 days)
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?

Answers (3)

Thomas Marullo
Thomas Marullo on 4 Dec 2015
Have you solved this problem? I am having the same problem.

Ryan Livingston
Ryan Livingston on 14 Feb 2017
Edited: Ryan Livingston on 14 Feb 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 on 29 Jan 2021
d = zeros (r,c)
d = fun_kk(u);
....

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!