MATLAB Answers

How to merge two bytes in matlab?

116 views (last 30 days)
SHRUTHI k
SHRUTHI k on 6 Oct 2016
Commented: Guillaume on 14 Oct 2016
Hello all, consider that I receive digital data in matlab from ESP8266 through serial port. The digital data that I get from ESP is 16 bit (2 bytes) data which I initially write it to serial port and then to a text file using matlab. But, in matlab when I acquire that data and write into a text file, at a time only 8 bits of data I can write. (ie.,a single 16 bits data will be written into text file as two separate 8 bits data in little endian format). Now the questions are, (1) when I want to plot the data written into that text file that i need to merge the two successive data and then plot right? (2) how I can achieve that?

  5 Comments

Show 2 older comments
SHRUTHI k
SHRUTHI k on 14 Oct 2016
@ Guillaume, the data is in HEX format. Initially I get the HEX data from ESP in serial port and I write them all into a text file (say example.txt). Then I use those data for further processing (One of them is plotting of those data).
@ Matthias Walle, I would like to show you a part of my text file containing HEX data. 1DC,31A,3,0,400,1,10D,38,2EB,3F1,7,0,231,0,134,39,203,30D,7,0,281,2,8C,2F,1D9,310,4,0,29A,3,198,3A,1D2,30F,1,0,2C4,0,105,31,1C5,315,2,0,2B7,1,13D,35,1E3,315,3,0,2F2,1,15C,3B,1E6,315,1,0,333,9,F0,34,1EC,31B,3,0,37C,3,14B,3C,1DA,31C,4,0,380,1,125,3E,1D4,317,1,0,356,6,0,35,1E7,318,4,0,38E,3,14A,35,1E6,311,2,0,3CC,5,D6,32,1FC,319,5,0,3D6,4,109,37,1E2,316,2,0,39D,3,11B,37,1CD,312,2,0,35C,3,126,2F,1E5,317,8,0,313,1,FE,37,1E7,319,5,0,2E6,0,142,34,1F2,31B,0,0,2AC,1,E,32,1DE,315,6,0,2CE,1,150,35,261,3B1,3,0,2B6,4,158,35,1EF,313,2,0,295,6,E2,36,1E5,315,3,0,2A9,6,155,36,1EB,315,1,0,29F,1,122,35,1E0,315,6,0,2C2,0,10B,33,1D1,317,3,0,2C3,2,143,33,1EC,31C,1,0,2EC,2,142,3A,1F4,319,4,0,2E1,6,FC,3C,1E8,318,3,0,2F6,4,143,3B,1CF,318,2,0,2D3,1,0,34,1DC,31A,3,0,2F8,2,E2,3C,1F2,30D,4,0,310,1,C0,31,1E0,315,4,0,308,1,14C,38,1E7,318,5,0,30B,2,131,39,1DA,318,3,0,312,1,10A,33,1D1,31A,1,0,301,3,127,32,1E2,31A,A,0,309,0,C1,32,1F4,400,0,0,22E,2,0,36,25C,31A,7,0,330,6,117,33,1E1,3E7,6,0,32D,1,FD,37,1A5,2D0,2,0,33B,2,105,33,1AF,2D4,3,0,328,2,112,38,1B9,2DB,7,0,33D,7,149,33,1BE,2DD,3,0,342,4,B5,30,1A4,2E7,2,0,33E,0,1A0,34,1B1,2F0,2,0,342,4,169,35,1C8,2F3,8,0,33F,1,9C,35,400,400,10,0,2FC,6,3,36,1C5,2B7,2,0,354,3,71,30,1BC,2C2,2,0,347,2,164,36,196,2CA,0,0,33C,4,15A,36,1B8,2D0,6,0,389,0,109,33,1B5,2D2,0,0,3AD,3,10C,31,1B9,2DF,2,0,3C3,3,14C,32,1AE,2DF,2,0,3E0,3,1B9,3C,1B7,2EB,3,0,3AF,4,1E2,33,1B9,2EE,5,0,33C,1,FF,39,1C2,2F7,0,0,32A,5,16F,35,1CC,304,0,0,2E6,1,2,35,1C2,2F2,2,0,31E,1,133,34,1B9,2FB,1,0,306,3,16B,38,1CB,2FC,3,0,33D,6,1B6,35,1D6,2FD,7,0,400,11,120,33,1CF,305,4,0,23A,5,1BF,34,1C7,302,0,0,251,8,1E0,36,1D3,30C,1,0,299,8,160,34,1D8,30D,6,0,2C4,4,F1,35,1E1,30C,1,0,2BF,2,E7,35,257,32C,3,0,2B7,2,106,39,1E8,30A,3,0,308,1,167,37,1CF,309,2,0,345,1,177,35,1E9,310,4,0,39E,2,105,36,1D7,30E,1,0,37B,4,178,37,1E2,30E,5,0,39C,3,181,33,1D3,315,3,0,3B9,3,11C,37,1DB,319,5,0,3C7,4,18,34,400,400,C,0,36A,1,0,39,1E7,2DD,2,0,38F,4,BD,30,1C7,2E2,3,0,327,0,14A,35,1B9,2E2,2,0,2F9,1,173,30,1B4,2E6,2,0,2DC,0,14F,35,1CF,2F0,3,0,2D6,1,1CD,38,1D0,2F6,2,0,2C7,0,131,34,1C9,2F7,
In the above, I want to merge first two data into one, next two data into one and so on. (don't worry about data separator which is a comma in my case. I will remove it.)
Guillaume
Guillaume on 14 Oct 2016
The sample of the text file that you show does not match the description you gave. 1DC, is not 8 bits, it's at least 24 bits. Each character of an hexadecimal string is 8-bit, so 3 characters is 24bits.
In the end, if you do want to merge 1DC with 31A, what number do you want: 1DC31A, 1DC031A, something else?
What about the 3 with the 0? Are they to be understood as 24-bit each as well to make 3000, 32-bits to make 30000, or just 8-bit to make 30?
When you received data from the serial port do you actually receive hex characters, that is you first receive the character '1' (whose 8-bit ASCII value is 31 in hexadecimal), then the character 'D' (whose 8-bit ASCII value is 44 in hexadecimal), etc. Or do you actually receive one byte whose ascii value is 1D?
Walter Roberson
Walter Roberson on 14 Oct 2016
Each hex character represents 4 bits, so 3 hex characters represents 12 bits. Merging adjacent pairs would require at least 24 bits.

Sign in to comment.

Answers (2)

Steven Lord
Steven Lord on 6 Oct 2016
Take a look at the typecast and swapbytes functions.

  0 Comments

Sign in to comment.


Guillaume
Guillaume on 6 Oct 2016
Edited: Guillaume on 6 Oct 2016
It's really not clear how you wrote the files to start with. If the digital data is a 16-bit integer, why (and how) do you write it as text?
Assuming, your data has been written in a binary file that is just a stream of bits, you can read it directly as numbers:
fid = fopen('C:\somepath\somefile', 'r', 'le'); %open the file as binary, little endian format
data = fread(fid, Inf, 'uint16'); %read all the file as 16-bit unsigned integer
fclose(fid);

  2 Comments

SHRUTHI k
SHRUTHI k on 14 Oct 2016
@ Guillaume, I already tried this but I got below error.
SIZE * PRECISION must be less than or equal to InputBufferSize. b = fread(s,cnt,'uint16');
Below is the code where I set input buffer size etc.
s = serial('COM9');
set(s, 'InputBufferSize',4096);
set(s, 'FlowControl', 'none');
set(s, 'BaudRate', 115200);
set(s, 'Parity', 'none');
set(s, 'DataBits', 8);
set(s, 'StopBit', 1);
But here I can't increase data bits from 8 to 16.(it gives another error).
Guillaume
Guillaume on 14 Oct 2016
I'm really lost as to what you're trying to do (see also my latest comment to your question). The answer I gave was about reading from a file, not a serial port.
Yes, you cannot set the DataBits property of a serial port to 16. The maximum is 8. Note that this has nothing to do with how you read the data.
If you want to read the data as 16-bit integer from the port, one integer at a time, then:
fread(s, 1, '*uint16')
will work. You can read more than one integer at a time, but your cnt must be less than or equal to inputBufferSize / 2 (as the error message told you), so 2048 is the maximum for cnt. You cannot use Inf for a serial port, that only works for files.
Note that for the above, I am assuming that your serial port is sending bytes, i.e. for example it is sending 01110101 in binary (73 in hex, 115 in decimal).
If you serial port is sending characters which are the encoding of bytes in hexadecimal (i.e. when transmitting the hex value 73, it first sends the character '7' (so the actual byte being transmitted is the ASCII value of '7' as hex = 37), then character '3' (Hex ASCII: 33), then the above is not the way to read your values.
Please clarify.

Sign in to comment.

Sign in to answer this question.


Translated by