# Run ANY matlab Function.m from simulink simulation

1 view (last 30 days)
David C on 4 Aug 2015
Commented: David C on 7 Aug 2015
Hello
I have a function written in Matlab and I want to run it from Simulink. The problem is that the MATLAB Function block in Simulink doesn't allow size changing arrays (which I have in the code), and when using matlab prescribed functions like "length", or "perms" (with the coder.extrinsic), the result is a mxArray which cannot be used for "while", etc.
I would like to know how can I run from Simulink ANY!!! Matlab function that runs perfectly in Matlab. (I tried using S-Function, but after many hours I could'nt really understand how does it works)

Mike Hosea on 4 Aug 2015
If you just want to run it from Simulink, just call your function as extrinsic. For example if your function is called myfun(a,b,c), and if myfun(a,b,c) returns, say, a 3-by-1 int32 vector, then your MATLAB function block would look something like
function y = fcn(a,b,c)
coder.extrinsic('myfun');
y = zeros(3,1,'int32'); % Define the output type of the myfun call.
y = myfun(a,b,c);
Just put your function in the same directory as the model or somewhere on the path.

David C on 5 Aug 2015
Thank You very much
I Still got something that doesn't work. lest say my input is for:
y=fcn(a,b,c)
Where "a" is a number. I need the output to be a 1-by-"a" vector. So I wrote the following line code, but it shows an error. How can I do the size of the array dependent on the input?
y = zeros(3,a,'double'); %(This line shows an error)
Mike Hosea on 5 Aug 2015
Either something is a true constant or it is not, and the trick is sometimes to make it more obvious to the block that it actually is a constant and not something that might change.
With just one exception, sizing something based on an input value to the block always produces an inherently variable-size array. Constant blocks in Simulink produce a constant signal, but they do not produce constants in the sense that an array of that length would be recognized as fixed-size by the MATLAB Function Block. To do that you will need to turn these input signals into non-tunable parameters, which can be done through the Model Explorer. That is the one exception. Once these inputs to the block are typed as parameters, their values can be fetched from the workspace. Unchecking the "tunable" box makes them true constants during the simulation (or for code generation), though you can easily change them and run a new simulation or generate code again with a different value.
More usually, the size of an output depends on the size of one or more inputs to the block. If, instead of an input value, you can use size(x), size(x,k), (for some explicit constant k), or length(x), where x is some data input to the block, and if that size/length is constant, then the block can usually figure out that it is constant. For example, if b and c are inputs to the block and a = size(b,2), then you would want to write
y = zeros(1,size(b,2),'double');
to define y as a row vector of length "a".
David C on 7 Aug 2015
Thank you very much!!!