Coder.Extrinsic Syntax Issues with TF property

2 次查看(过去 30 天)
Hello,
I am gettign the following error while using matlab function block in simulink.
Expected either a logical, char, int, fi, single, or double. Found an mxArray. MxArrays are returned from calls to the MATLAB interpreter and are not supported inside expressions. They may only be used on the right-hand side of assignments and as arguments to extrinsic functions.
to the following code snippet:
%#codegen
coder.extrinsic('tf');
P = 1; I= 1; dtsys = 0.1
z = tf('z',dtsys);
iterm = tf()
Iterm = tf(1,[1 -1],dtsys)
K = P + I*dtsys*Iterm;
I did find some related questions in the forum but they are handled by pre-assigining the value with the class, such as double(0) etc. In this case, how do I assign the tf class.

回答(1 个)

Jaynik
Jaynik 2024-1-11
Hi Abhijit,
It seems that the error you're encountering is because the "tf" function does not support code generation. The function produces a "mxArray", which is not a codegen-compatible type.
To resolve this issue, you'll need to avoid using "tf" and other functions that are not supported for code generation within the "MATLAB Function" block. Instead, you should define your transfer function using a discrete-time representation that is compatible with code generation, such as using state-space, zero-pole-gain models, etc.
If your application just needs the numerator and the denominator values of the variable "K", you can check the following code:
% Define your constants
P = 1;
I = 1;
dtsys = 0.1;
num_Iterm = I * dtsys; % Numerator of Iterm (multiplied by z^0)
den_Iterm = [1, -1]; % Denominator of Iterm (1 - z^-1)
num_P = P; % Numerator of Pterm (P multiplied by z^0)
den_P = 1; % Denominator of Pterm (1)
% Multiply the proportional term by the denominator of the integral term
num_P_aligned = num_P * den_Iterm; % P * (1 - z^-1)
% Now we can add the numerators directly
num_combined = num_P_aligned + [0, num_Iterm]; % Adding zero to align with z^-1 term
% The denominator remains the same as the denominator of the integral term
den_combined = den_Iterm;
You can use these coefficients to implement the transfer function using a "Discrete Transfer Function" block or a "Discrete State-Space" block in Simulink, which are both compatible with code generation.
You can check the following documentation to learn more about each block:
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 General Applications 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by