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

5 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

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);

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products


Release

R2013a

Community Treasure Hunt

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

Start Hunting!