Run ANY matlab Function.m from simulink simulation
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
Mike Hosea
le 4 Août 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 commentaires
Mike Hosea
le 5 Août 2015
Modifié(e) : Mike Hosea
le 5 Août 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".
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Simulink Functions dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!