How can I use anonymous function / cfit in simulink ?

3 ビュー (過去 30 日間)
Ezor
Ezor 2017 年 1 月 10 日
コメント済み: James Whidborne 2021 年 2 月 20 日
Hello everyone,
I have created several models stored as anonymous functions or cfit objects (I can change for one another, depending on what is the best in that case) in the main workspace.
I would like to use them in Simulink, namely take as input a signal u, and use my function f on it f(u) = y where y will be the output signal in simulink.
So far, I have tried to call them in Matlab Function Block but it doesn't work. For instance, with coder.extrinsic
function y = fcn(u)
y=0;
coder.extrinsic('evalin')
coder.extrinsic('my_fun_in_WS')
fun = evalin('base','my_fun_in_WS');
y = fun(u);
end
It seems that u has not the right type since I get this error Function output 'y' cannot be an mxArray in this context. Consider preinitializing the output variable with a known type.
However, if I put
y = feval(fun,42);
Then the output is correct (constant, equal to fun(42))
Do you have any solution to workout this issue?
Thank you in advance (and sorry for bad english)
Edward
  1 件のコメント
James Whidborne
James Whidborne 2021 年 2 月 20 日
I find it also works ok using an Interpreted MATLAB Fcn block

サインインしてコメントする。

回答 (1 件)

Sean de Wolski
Sean de Wolski 2017 年 1 月 12 日
編集済み: Sean de Wolski 2017 年 1 月 13 日
See my answer here:
EDIT In R2016b, anonymous functions are supported for code generation and thus can be used directly inside of the MATLAB function block:
For cfit, sfit objects you will still need to call those inside of a separate MATLAB function labeled as coder.extrinsic in the MATLAB function block.
  4 件のコメント
Ezor
Ezor 2017 年 1 月 16 日
Unfortunatly, I have to work on Matlab2014a, but it's good to know !
Wes Yeagley
Wes Yeagley 2018 年 1 月 22 日
Could you show an example where you use a cfit object in Simulink. I am having trouble calling the cfit object in a separate Matlab function and thus cannot use it in Simulink.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSimulink Functions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by