How do I call an anonymous function inside the MATLAB function block in Simulink 8.1 (R2013a)?

39 views (last 30 days)
I have a model which has the MATLAB function block. I want to be able to call the FMINBND anonymous function within this block.I understand that this is a function that is not within the list of supported functions for the MATLAB function block and so I use coder.extrinsic to declare FMINBND as follows:
function y = fcn()
%#codegen
coder.extrinsic('fminbnd')
y = fminbnd(@(x) x.^2+100-650, 0, 100);
end
However, I get the following error when I run the model:
This kind of expression is not supported.
How do I get around this?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 23 Apr 2018
Embedded MATLAB does not support anonymous functions, as documented in the documentation page "MATLAB Language Features Supported for C/C++ Code Generation":
>>web([docroot '/simulink/ug/matlab-language-features-supported-for-code-generation.html']) 
However, you can workaround the issue by creating a seperate MATLAB function, say myFunc.m, that calls the anonymous function FMINBND and then using coder.extrinsic to declare this myFunc.m and use it. This is illustrated in the example code below:
function y = fcn()
%#codegen
y = 0;
coder.extrinsic('myFunc')
y = myFunc();
end
Where myFunc.m is:
function a = myFunc()
a =fminbnd(@(x) x.^2+100-650, 0, 100);
end
  3 Comments
Berkin Birol
Berkin Birol on 21 Apr 2018

I have a question about your answer.

I try to use my existing function (try to model an automata with 4 states) in Simulink and I have multiple inputs and outputs. First I was using interpreted Matlab Function, but it is complicated to arrange all inputs with one mux (when try to increase or decrease number of states). So, I tried your "coder.extrinsic('myfun')" solution with MATLAB Function block, it worked but I have one little issue. My function have inputs as arrays, two 1x4 inputs and one 1x4 output. MATLAB function deals with those two 1x4 inputs, but gives error with the one output.

Where is my mistake?

my Function:

function Q = fsm_yeni_2Giris(t,q)
if sum(q)==0
    Q(1)=1;
end
n=4; 
for i=1:n-1
if q(i)==1  && t(i)==1
    Q(i+1)=1;
    Q(i)=0;
end
end
if q(n)==1 && t(n)==1
Q(1)=1;
Q(n)=0;
elseif Q(n)~=1
    Q(n)=q(n);
end
end

MATLAB function in Simulink:

function y = fcn(t,q)
%#codegen
y=0;
coder.extrinsic('fsm_yeni_2Giris')
y = fsm_yeni_2Giris(t,q);

my Simulink model:

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 21 Apr 2018
Edited: Walter Roberson on 21 Apr 2018

If sum(q) == 0 because all of the entries are 0, then you only store into Q(1) and none of the other locations in Q.

Remember that the first assignment to a variable determines the size of the variable, so the first thing you should do is

Q = zeros(1,n);
  1 Comment
Berkin Birol
Berkin Birol on 22 Apr 2018

Thanks for the recommendation. But I don't want to set Q as a 1xn zero vector everytime the block runs. I need to update just the parts needs to be changed. So, I added this instead, but still didn't work:

n=4;
for i=1:n
Q(i)=q(i);
end

or just

n=4;
Q=q;

q is a 1x4 vector, so Q must be 1x4 too.

function Q = fsm_yeni_2Giris(t,q)
n=4;
for i=1:n
Q(i)=q(i);
end
%%
if sum(q)==0
   Q(1)=1;
end
%%
for i=1:n-1
if q(i)==1  && t(i)==1
    Q(i+1)=1;
    Q(i)=0;
end
end
%%
if q(n)==1 && t(n)==1
Q(1)=1;
Q(n)=0;
elseif Q(n)~=1
    Q(n)=q(n);
end
end

Am I missing something?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!