Why do i get an error with coder.extrinsic?
11 views (last 30 days)
Show older comments
Volkan Yangin
on 8 Sep 2021
Commented: Volkan Yangin
on 8 Sep 2021
Hi,
I have very complicated matrices and vectors which consist of "dirac". To run dirac command in SIMULINK, i use coder.extrinsic('dirac'), nevertheless my block cannot work. SIMULINK gives an error:
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.
Function 'MMM' (#228.1467.1878), line 11, column 1172:
"0.027696559278073379663632195329385*dirac(tan(beta - (1.0*lr*yaw_angle)/v_x) - 0"
Launch diagnostic report.
My code:
function [A_delta,B_delta,C_delta,D_delta] = fcn(u,beta, w_z, yaw_angle, lat_pos,mu_road,v_x,m_total,lf,lr,J_z)
coder.extrinsic('dirac');
A_delta = (zeros(4,4))
B_delta = (zeros(4,1))
C_delta = (zeros(1,4))
D_delta = 0;
A_delta = % 4 x 4 matrix, it is too long to write here. Some of the lines have dirac commands like above.
B_delta = % 4 x 1 vector
C_delta = %1 x 4 vector
D_delta = 0
0 Comments
Accepted Answer
Walter Roberson
on 8 Sep 2021
Every single place you have a call to dirac(), you need to move it to be an assignment to a variable by itself. For example,
y = 3 + beta*dirac(w_z - 5) + beta^2*dirac(w_z + 5)
then you would have to do
dm5 = dirac(w_z - 5);
dp5 = dirac(w_z + 5)
y = 3 + beta*dm5 + beta^2*dp5;
Caution:
dirac() is part of the Symbolic Toolbox, and it is not possible to generate code for any part of the Symbolic Toolbox. You can use them with coder.extrinsic() with either optimization turned off completely (where Simulink asks MATLAB to process all expressions), or with optimization set to the lowest level (where SImulink compiles what it knows how to do and asks MATLAB to process the rest), but not for higher optimization levels.
You should consider writing your own dirac() :
dirac = @(x) double(x == 0)
3 Comments
Walter Roberson
on 8 Sep 2021
Initialize all of those new variables to 0 with the appropriate size. In the above brief example it might end up looking like
dm5 = zeros(1,17);
dp5 = zeros(1,17);
dm5 = dirac(w_z - 5);
dp5 = dirac(w_z + 5)
y = 3 + beta*dm5 + beta^2*dp5;
The pre-allocation causes Simulink to construct an array for the result, and then when the call to dirac() is made by itself returning an mxArray, Simulink is able to copy the mxArray contents into the pre-allocated variable.
Yes, I know from the point of view of MATLAB's dynamic allocation that it feels wasteful to assign zeros into an array only to have it immediately overwritten, but the assignment of zero is really a pre-allocation step that Simulink is able to reason about to figure out what size of array to expect the dirac() call to return.
More Answers (0)
See Also
Categories
Find more on Configure and View Diagnostics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!