How to convert byte number into float in Simulink

조회 수: 14 (최근 30일)
Tejas
Tejas 2023년 9월 28일
댓글: Tejas 2023년 9월 29일
Hello,
In my model, I have input as byte number and need to convert into float number, how can I do this in MATLAB simulink?
like, input is 2 byte and need to convert to float variable so I can compare this number with another floating number in simulink.. without this my generated code is not working correctly. I need to convert byte into float.
Any feedback is appreciated.
Thank you in advance!

답변 (1개)

Walter Roberson
Walter Roberson 2023년 9월 28일
  댓글 수: 3
Walter Roberson
Walter Roberson 2023년 9월 29일
I have no reason to expect that for the fundamental types that the generated code is doing anything other than cast or just directly calling single() or double()
If you have reason to double, then create a MATLAB Function Block along the lines of
out = function fcn(u1)
out = single(u1); %or double() as appropriate
end
Question: is your input possibly an array of uint8? If so then
out = function fcn(u1)
temp16 = typecast(u1, 'uint16'); %or int16 as appropriate
out = single(temp16); %or double() as appropriate
end
If it is an array of uint8 then you might potentially need to swapbytes(temp16)
Mind you you might prefer the simplicity of
out = function fcn(u1)
out = single(u1(1)) * single(256) + single(u1(2));
end
which allows you to easily exchange the (1) and (2) if the data is in the other byte order. The expression is a bit longer if you are using signed integers.
Tejas
Tejas 2023년 9월 29일
Thanks for the explanation.. I will try this way.. hopefully work out..

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Sources에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by