Run ANY matlab Function.m from simulink simulation
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
Mike Hosea
2015년 8월 4일
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
Mike Hosea
2015년 8월 5일
편집: Mike Hosea
2015년 8월 5일
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".
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Simulink Functions에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!