Run ANY matlab Function.m from simulink simulation

1 visualización (últimos 30 días)
David C
David C el 4 de Ag. de 2015
Comentada: David C el 7 de Ag. de 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)
Thanks in advance

Respuesta aceptada

Mike Hosea
Mike Hosea el 4 de Ag. de 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.
  3 comentarios
Mike Hosea
Mike Hosea el 5 de Ag. de 2015
Editada: Mike Hosea el 5 de Ag. de 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
David C el 7 de Ag. de 2015
Thank you very much!!!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Simulink Functions en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by