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

64 ビュー (過去 30 日間)
Matthias Schmidt
Matthias Schmidt 2022 年 8 月 23 日
コメント済み: Matthias Schmidt 2022 年 8 月 25 日
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

採用された回答

Andy Bartlett
Andy Bartlett 2022 年 8 月 23 日
編集済み: Andy Bartlett 2022 年 8 月 23 日
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.
  3 件のコメント
Andy Bartlett
Andy Bartlett 2022 年 8 月 23 日
@Walter Roberson Good point
Matthias Schmidt
Matthias Schmidt 2022 年 8 月 25 日
Thank you...this helped a lot

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSimulink Functions についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by