This assignment writes a 'int16' value into a 'double' type.

52 views (last 30 days)
Hi,
I use the code below in a function. My input is int16 data type and I assigned this in the code. But I still get the error: This assignment writes a 'int16' value into a 'double' type.
Best regards
function y = fcn(u)
%Input
IC=1;
bufferlength=8000;
persistent accxsignal;
if isempty(accxsignal)
if isequal(numel(IC),bufferlength)
accxsignal = IC;
elseif isscalar(IC)
accxsignal = IC*ones(1,bufferlength);
else
error('IC must either be scalar or the same dimensions as bufferlength')
end
end
%Output
y = int16(accxsignal);
%Update
accxsignal = [u accxsignal(1:end-1)];
end

Accepted Answer

Andy Bartlett
Andy Bartlett on 23 Aug 2022
Edited: Andy Bartlett on 23 Aug 2022
My guess is you are using this code in a situation that explicitly or implicitly requires generation to C/C++ code such as
MATLAB Coder codegen
Simulink's MATLAB Function Block
Simulink's MATLAB System Block
Stateflow MATLAB action language
These situations do not allow you to change the data type of a variable from one assignment to the next.
In your code,
accxsignal = IC*ones(1,bufferlength);
will assign doubles to accxsignal.
Because u is uint8, this line
accxsignal = [u accxsignal(1:end-1)];
will try to change accxsignal to uint8. That's not allowed in explicit or implicit code generation situations.
To solve this, your friends are "colon assignment" and "cast like".
For all "assignments" to a variable after the very first one, use "colon assignment" or "index assignment" to write a value into the existing variable rather than completely redefine the variable.
foo(:) = goo; % 2nd assignment and beyond
Use cast-like to set a variable or constants type based on another variable.
var2 = cast( 13, 'like', var1 );
For example:
function y = fcn(u)
%Input
% IC same type as u
IC= cast( 1, 'like', u);
bufferlength=8000;
persistent accxsignal;
if isempty(accxsignal)
% First assignment to accxsignal
% sets the type same as u
% because IC is same type as u
%
if isequal(numel(IC),bufferlength)
accxsignal = IC;
elseif isscalar(IC)
accxsignal = repmat( IC, 1,bufferlength );
else
error('IC must either be scalar or the same dimensions as bufferlength')
end
end
%Output
y = int16(accxsignal);
%Update
% Use Colon-assignment to prevent changing type of accxsignal
% Given the changes above,
% I believe this is not really necessary in this example
% because the right hand side of the next line will be same type as u
% so "regular assignment" would not change type in this specific case.
% BUT, it is a good habit for code gen usage to use "colon-assignment"
% for all "assignments" after the first one.
%
accxsignal(:) = [u accxsignal(1:end-1)];
end
Again, I'm guessing your were in an explicit or implicit code generation situation. If that's not true, then these suggestion likely do not apply.

More Answers (0)

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by