Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

How can I convert text file to binary so that the final array is of integer or double format?

Asked by Shubhanka on 16 Mar 2013

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

Shubhanka

Products

No products are associated with this question.

2 Answers

Answer by I Made on 16 Mar 2013
Edited by 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.

0 Comments

I Made
Answer by Cedric Wannaz on 16 Mar 2013
Edited by Cedric Wannaz 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

Cedric Wannaz

Contact us