Can I put an anonymous function into a Simulink block
조회 수: 17 (최근 30일)
이전 댓글 표시
Although pretty good with Matlab, I’m a total amateur with Simulink so hoping someone can help.
I have a Matlab function y = fun1(x,M) that I want to put into a Simulink block (and later create an S-function version). x will come from another block in the Simulink model. M is a structure with a lot of information; it doesn’t change and I want to embed it in the Simulink block. I don’t want the block to have to load M from a matfile or call additional functions each time.
So, I thought to create an anonymous function as follows: anon_fun = @(x)fun1(x,M);
And then put the anon_fun in a Matlab Function, fcn or similar Simulink block. But none of these seem to work!
Is it possible to embed an anonymous function in a Simulink block, which type, and will I be able to generate an S-function from it?
Thanks in advance for any tips.
댓글 수: 0
채택된 답변
Mandar Patwardhan
2016년 12월 22일
If your use case is to create S-function, then refer to the following link which discusses the detailed steps to create a Level 2 MATLAB S-function. https://www.mathworks.com/help/simulink/sfg/writing-level-2-matlab-s-functions.html#brgtux6
You can create an S-function block, as shown in the example, add it in your model and provide a signal representing x as an input to this block.
If you do not intend to change the structure represented by M, then you can use a persistent variable and directly use it inside an S-function. There is no need to pass M as a parameter in that case because you will always have access to M inside the function. For information regarding persistent variables refer to the following link. https://www.mathworks.com/help/matlab/ref/persistent.html Additionally, it is not clear why would you use Anonymous functions inside a MATLAB function block. The use case which you are discussing can be definitely achievable using a Level-2 MATLAB S-function.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Simulink Functions에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!