Azzera filtri
Azzera filtri

How to convert byte number into float in Simulink

17 visualizzazioni (ultimi 30 giorni)
Tejas
Tejas il 28 Set 2023
Commentato: Tejas il 29 Set 2023
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!

Risposte (1)

Walter Roberson
Walter Roberson il 28 Set 2023
  3 Commenti
Walter Roberson
Walter Roberson il 29 Set 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.
Tejas
Tejas il 29 Set 2023
Thanks for the explanation.. I will try this way.. hopefully work out..

Accedi per commentare.

Categorie

Scopri di più su Subsystems in Help Center e File Exchange

Tag

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by