How can I convert text file to binary so that the final array is of integer or double format?
31 views (last 30 days)
Show older comments
I need to import a text file into matlab and convert it into binary. The final array that contains the binary values has to be in some integer or double format and not character format. I tried using dec2bin, but by using dec2bin the format changes to character. How can I convert ascii values to binary and make it stay as integer values?
0 Comments
Accepted Answer
Cedric
on 16 Mar 2013
Edited: Cedric
on 16 Mar 2013
Any variable/data is stored as a binary code (taking one or multiple bytes) in memory. The way to interpret this code, for a given variable, is defined by the type/class of the variable. When you create, for example
>> a = uint8(5) ;
it stores the unsigned 8 bits code that corresponds to the integer 5 somewhere in memory, using a byte (=8 bits). You have therefore 00000101 (that codes 2^0+2^2 = 1+4 = 5) stored at some address..
0x.... [00000101]
Now when you type the name of the variable in the command window, MATLAB reads this byte, know the type of the variable, and is able to interpret the code and display it as an integer for you:
>> a
a =
5
you can by the way look at what MATLAB knows about this variable using WHOS
>> whos
Name Size Bytes Class Attributes
a 1x1 1 uint8
Now you can operate directly on the binary code that a points to using bit-wise operators, e.g. getting the least significant bit (the first bit of the binary code, starting from the right)
>> bitget(a, 1)
ans =
1
or get all the 8 bits
>> bitget(a, 8:-1:1)
ans =
0 0 0 0 0 1 0 1
you can set bits as well (or compare, or perform bit-wise logical operations)
>> bitset(a, 2, 1) % Sets bit #2 to 1 -> build 00000111b = 7d.
ans =
7
You can also get a string made of characters '0' and '1' that represent the binary code:
>> dec2bin(a)
ans =
101
but this is a string
>> class(ans)
ans =
char
it is only a representation, useful almost only for display. You can use it, however, by subtracting the character '0' (subtracting its code to all characters of the string), which generates an array of double 0's and 1's. This is comparable to what you get directly using BITGET (yet, the latter returns an array of uint8 0's and 1's)
>> dec2bin(a, 8)-'0'
ans =
0 0 0 0 0 1 0 1
>> class(ans)
ans =
double
Note that both solutions (BITGET, or DEC2BIN()-'0') output arrays that contain the same information as the original variable, but that are much bigger in memory
>> a_bin_uint8 = bitget(a, 8:-1:1) ;
>> a_bin_dbl = dec2bin(a, 8)-'0' ;
>> whos
Name Size Bytes Class Attributes
a 1x1 1 uint8
a_bin_dbl 1x8 64 double <== 64 times!
a_bin_uint8 1x8 8 uint8
In text files, numbers are stored/represented as characters; you generally want to convert them into numeric values ( integer, double, etc). You can do this using SSCANF, FSCANF, TEXTSCAN, STR2NUM, STR2DOUBLE, etc .. any function that either has a formatSpec parameter where you can specify a numeric type, or whose purpose is converting strings to numbers.
When you perform such an operation, e.g.
>> b = str2double('4.56')
b =
4.5600
>> whos
Name Size Bytes Class Attributes
a 1x1 1 uint8
b 1x1 8 double
MATLAB creates a variable with the relevant numeric type, and performs the conversion from string. In memory, this will e saved as a binary code, but you usually don't want to interpret it by yourself (as it means understanding the coding specific to the numeric type, which can be a bit complicated). It is generally enough for you to let MATLAB manage this part, and use a variable already interpreted by MATLAB, as a double for example.
I don't know if that fully answers your question, but it might help you understanding better what you are doing (or what you should do) when you want to import text files content and convert it into numerical types in order to perform computations.
0 Comments
More Answers (2)
I Made
on 16 Mar 2013
Edited: I Made
on 16 Mar 2013
Try This :
fid = fopen('example.txt');
x=fread(fid,'*char');
binary = dec2bin(x,8);
binary_t=transpose(binary);
bin=binary_t(:)-'0';
The result stored in bin as double, i haven't change the code yet so you just have to adjust to your use. Feel free to comment if something i cen help you more with, hope this help.
2 Comments
Miguel A Morales V
on 10 Apr 2018
In this code, what's the usefullness of to put -'0'? I catch a better output when I get back to text
Nikesh Bajaj
on 3 Jan 2016
After reading file with fopen, Use file text2bin file which convert text to binary and binary to text.
0 Comments
See Also
Categories
Find more on Data Type Conversion 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!