How to convert four bytes into double ?

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);

 Accepted Answer

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 Comments

Thanks for the reply Mr.Walter Roberson. Kindly guide me to solve below problem
a = tcpclient('192.168.10.4',2000,'Timeout',10,'ConnectTimeout',30); %Establish Communication between client & server
Data = read(a,4,"uint8"); %Read 4 Byte Values from Server
we have some limitation in Server side. it supports only Bytes data type for both send / receive operation.
Here i used the below code for Offline Record and Plot option:
%% Record and plot 10 seconds of temperature data
ii = 0;
TempF = zeros(1e4,1);
t = zeros(1e4,1);
tic
while toc < 10
ii = ii + 1;
v = read(a,4,"uint8");
TempF(ii) = typecast(v,'uint32');
t(ii) = toc; % Get time since starting
end
% Post-process and plot the data. First remove any excess zeros on the
% logging variables.
TempF = TempF(1:ii);
t = t(1:ii);
% Plot temperature versus time
figure
plot(t,TempF,'-o')
xlabel('Elapsed time (sec)')
ylabel('Temperature (\circF)')
title('Ten Seconds of Temperature Data')
set(gca,'xlim',[t(1) t(ii)])
---------------------------------------------------END-------------------------------------------------------------
In above code, i m converting 4 bytes of uint8 to uint32(single value and not an array).
This code is working fine and here i attached the plot ( i send values 500 from server --> client and it generate plots correspondingly)
But my main requirement is to do Live plotting Time (vs) Temperature. unfortunatly my temperature value should contains max 4 bytes of data (Min 2 bytes) .thats y i used to read 4 bytes and see in above code:
v = read(a,4,"uint8");
TempF(ii) = typecast(v,'uint32');
while doing Live Plotting, i need to use "addpoints" command. it supports only double data types for X & Y Axis.
we have max 4 bytes of temperture data ! how to convert 4 bytes of array uint8 to one double value (single value and not an array)
FOR LIVE PLOTTING CODE HERE:
figure
h = animatedline;
ax = gca; % Current Axes
ax.YGrid = 'on';
ax.YLim = [0 500];
stop = false;
startTime = datetime('now');
while ~stop
v = read(a,4,"uint8");
TempF = typecast(v,'double');
t = datetime('now') - startTime;
xx = datenum(t);
addpoints(h,xx,TempF);
ax.XLim = datenum([t-seconds(15) t]);
datetick('x','keeplimits')
drawnow
end
_-------------------------------------------------------END------------------------------------------------
Error using typecast
The first input must contain a multiple of 8 elements to convert from uint8 (8 bits) to double (64
bits).
===> it is showing error that dimensions not matching.. how to solve this ? need your help sir !!
### i want to convert 4 bytes of array uint8 to one double value (single values and not an array) for the purpose of using addpoints command then only i will make live plotting
please can you show any reference ?
"unfortunatly my temperature value should contains max 4 bytes of data (Min 2 bytes)"
What datatype is the data being generated on the device, and how is that data being encoded for transmission?
Your "Min 2 bytes" concerns me: that tends to imply that the data is text representation of numbers, such as '-7.03' or '42' or '1018' instead of being in binary (int32 or uint32 or single precision)
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
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.

Sign in to comment.

More Answers (1)

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 Comments

I suspect you intended
h = animatedline(NaT-NaT, uint8(NaN));
I did, thank you for catching the typo! I am editing the original answer so that it isn't copied incorrectly.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!