Resolve Error: Data Type Mismatch
Issue
In this example, y uses the default fimath
setting FullPrecision
for the SumMode
property. At each iteration of the for-loop in the function
mysum
, the word length of y grows by one
bit.
During simulation in MATLAB®, there is no issue because data types can easily change in MATLAB. However, a data type mismatch error occurs at build time because data types must remain static in C.
Possible Solutions
Rewrite the function to use subscripted assignment within the for-loop.
In this example, rewrite y = y + x(n) as y(:) = y + x(n), so that the value on the right is assigned in to the data type of
y. This assignment preserves the
numerictype
of y and avoids the type
mismatch error.
Original Algorithm | New Algorithm |
---|---|
Function: function y = mysum(x,T) %#codegen y = zeros(size(x), 'like', T.y); for n = 1:length(x) y = y + x(n); end end | Function: function y = mysum(x,T) %#codegen y = zeros(size(x), 'like', T.y); for n = 1:length(x) y(:) = y + x(n); end end |
Types Table: function T = mytypes(dt) switch(dt) case 'fixed' F = fimath('RoundingMethod', 'Floor') T.x = fi([],1,16,11, F); T.y = fi([],1,16,6, F); end end | Types Table: function T = mytypes(dt) switch(dt) case 'fixed' F = fimath('RoundingMethod', 'Floor') T.x = fi([],1,16,11, F); T.y = fi([],1,16,6, F); end end |