How to convert four bytes into double ?

26 visualizaciones (últimos 30 días)
Parthiban
Parthiban el 14 de Oct. de 2022
Editada: Noah Prisament el 19 de Jul. de 2023
Hai, my requirement is mentioned below;
i m using Ethernet TCP IP communication and handshake was done sucessfully.
Then am using animated line plot with "addpoints" command. In addpoints command -> it supports only "double" data type for X & Y Axis, but my input value for Y axis is four bytes / two bytes.
How to convert two or four byte into double ? any byte swap need to be done ? if yes means pls comment your suggestion !!
here i will add my Matlap code:
a = tcpclient('192.168.10.4',2000,'Timeout',10,'ConnectTimeout',30); %Establish Communication
Data = read(a,8,"uint8"); %Read Values from Server
figure
h = animatedline;
ax = gca; % Current Axes
ax.YGrid = 'on';
ax.YLim = [0 500];
stop = false;
startTime = datetime('now');
while ~stop
Data = read(a,8,"uint8"); %%%% i need to convert double
t = datetime('now') - startTime;
xx = datenum(t);
addpoints(h,xx,Data);

Respuesta aceptada

Walter Roberson
Walter Roberson el 14 de Oct. de 2022
Editada: Walter Roberson el 14 de Oct. de 2022
See typecast and also swapbytes (which you would need if the sender is sending "Big Endian")
However, is there any reason you do not simply do
Data = read(a, 1, "double");
possibly with a swapbytes() ?
  5 comentarios
Parthiban
Parthiban el 15 de Oct. de 2022
my data format is LREAL / int16.
we are using TCP IP communication between PLC (Server) & MATLAB CLIENT and transfering data through byte format.
That byte transmission & Reception working fine only sir. i need to convert 4 bytes of uint8 (array 1*4) to one double values (Single value) and fetch those values to "Addpoints" command and to do live plotting
Walter Roberson
Walter Roberson el 15 de Oct. de 2022
Could you give an example of the sequence of 4 bytes, and the corresponding numeric value that you expect?
Also I am still concerned about how sometimes you do not have exactly 4 bytes ? Please explain that further.

Iniciar sesión para comentar.

Más respuestas (1)

Noah Prisament
Noah Prisament el 19 de Jul. de 2023
Editada: Noah Prisament el 19 de Jul. de 2023
The "animatedline" now supports all numeric datatypes along with datetimes and durations natively as of R2023a!
In order to plot your data on an "animatedline" you can now utilize the following syntax:
h = animatedline(NaT-NaT, uint8(NaN));
addpoints(h,xx,Data);
You also don't have to convert the duration to a datenum anymore either. Use NaT-NaT for for a duration and NaT for datetimes.
  2 comentarios
Walter Roberson
Walter Roberson el 19 de Jul. de 2023
I suspect you intended
h = animatedline(NaT-NaT, uint8(NaN));
Noah Prisament
Noah Prisament el 19 de Jul. de 2023
Editada: Noah Prisament el 19 de Jul. de 2023
I did, thank you for catching the typo! I am editing the original answer so that it isn't copied incorrectly.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by