How to convert byte number into float in Simulink
16 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Walter Roberson
on 28 Sep 2023
https://www.mathworks.com/help/simulink/slref/datatypeconversion.html Data Type Conversion block
3 Comments
Walter Roberson
on 29 Sep 2023
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.
See Also
Categories
Find more on Subsystems in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!