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

2 次查看(过去 30 天)
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?

回答(3 个)

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

Ryan Livingston
Ryan Livingston 2017-2-14
编辑:Ryan Livingston 2017-2-14
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 2021-1-29
d = zeros (r,c)
d = fun_kk(u);
....

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by