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

### Accepted Answer

##### 3 Comments

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:

### More Answers (1)

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

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?

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!