How to convert byte number into float in Simulink

16 次查看(过去 30 天)
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