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!
0 个评论
回答(1 个)
Walter Roberson
2023-9-28
https://www.mathworks.com/help/simulink/slref/datatypeconversion.html Data Type Conversion block
3 个评论
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Sources 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!