2
votes

I have something similar to the following block diagram on Simulink, which looks rather messy especially with an increasing number of blocks.

enter image description here

I want to replace a 3-point summing block with a function block, while keeping the same output.

enter image description here

First I started by placing the code inside the function block:

function y = fcn(u)
   sys1 = tf(0.5,[1 0 0 4]);
   sys2 = tf([3 0.5],[1 0 15]);
   sys3 = tf(1,[1 1]);
y = sys1 + sys2 + sys3;

However I was greeted with an error saying that Simulink does not support code generation.

"The 'tf' class does not support code generation."

I then came across a similar problem here: https://nl.mathworks.com/matlabcentral/answers/74770-is-there-any-way-to-disable-code-generation-in-simulink

I am trying to implement an extrinsic function or 'wrapper function' with some difficulty. I created a new script called myWrapper.m, containing the same code:

function y = myWrapper(u)
   sys1 = tf(0.5,[1 0 0 0 4]);
   sys2 = tf([3 5],[1 0 15]);
   sys3 = tf(1,[1 1]);
y = sys1 + sys2 + sys3;

and the MATLAB Function edited to:

function y1 = fcn(u1)

y1 = myWrapper(u1);

The error persists.

I somehow want to access myWrapper.m file from the MATLAB Function block. Any pointers on how this should be done? Following the previous link given and the official docs I am ending up with something like this in my MATLAB Function block:

function y1 = fcn(u1)coder.extrinsic('myWrapper')

y1 = myWrapper(u1);

The last code above is syntactically incorrect and I am at a loss on how it should be done. MATLAB automaticaly corrects the above code to:

function y1 = fcn(u1,coder,extrinsic, myWrapper )

y1 = myWrapper(u1);

which is not what I want.

Any tips and/or suggestions on how this could be done would be appreciated.

A similar question was asked on the MathWorks forum here, two years ago, with no response.

1
You may just not be able to do this, the idea of having it explicit, I think, its because the compiler does not want suprisesAnder Biguri
@AnderBiguri, many thanks for your contribution. I caught your first comment too, and I think that you were correct in pointing that out. Regarding the comment above, I am almost certain this can be done, or else was allowed in a previous version of Simulink/MATLAB.rrz0
2017a. I will edit the question to give a broader view of what I am trying to accomplish.rrz0
Okay, I'll correct myself. It won't lead to any results, because your transfer function objects do not use the block input uas input. These objects cannot interact with the Simulink Solver at all... Still you did not answer my question, why you do not use the model as it is displayed. The model does not look messy at all... At work I see messed up models all the time. You would not believe.Sven Krüger
As commented by @Sven, your code won't replicate your model for at least 2 reasons - you are not passing the input through the transfer functions, and even is you were the states in the tf objects are not interacting with the Simulink solver (while the ones in the original Simulink blocks are). However, to address your specific issue, the coder.extrinsic('myWrapper') part of your code needs to be on a separate line to the function y1 = fcn(u1) part.Phil Goddard

1 Answers

0
votes

I was going about tackling this problem completely wrong. Thanks to several helpful comments I realized that in order to replace the summing block, one must NOT remove the Transfer Function blocks which feed into the summing block.

A MATLAB Function does not support code generation (and rightly so) such that a transfer function may be implemented inside it. That is why the blocks simply feed into the MATLAB Function as follows.

enter image description here

The script would very simply be:

function y1 = fcn(u1, u2, u3)

   x = (u1 + u2 +u3);
   y1 = x;

end