Binary in Matlab

Discussion in 'Programmer's Corner' started by Rob_K, Jan 12, 2014.

  1. Rob_K

    Thread Starter New Member

    Jan 12, 2014
    2
    0
    Hi there,

    I am trying to create a file of approximately 2048 lines of 16-bit binary, it doesn't matter what the data is, it is just test file. so far, I have come out with this:

    x = 0:1:1024;
    y = 32767*sin(x);

    fid = fopen('data.txt','w');
    fprintf(fid, '%5.0d\n', y);
    fclose(fid);

    but it writes it to the file in the format:
    3e+04

    I am aware that the data generated using a sine wave like I have, will likely have a decimal point, but is there not a way to round it up to the
    nearest whole number and not have it is scientific notation. ie:

    -32768 to 32767

    As I said if it could be in the format:

    0000 0000 0000 0000

    that is what I really want. Any help on this would be really helpful, I'm sure it is just a simple thing.

    Kind regards
    Rob
     
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,399
    497
  3. Rob_K

    Thread Starter New Member

    Jan 12, 2014
    2
    0
    Perfect, thank you shteii01
     
  4. WBahn

    Moderator

    Mar 31, 2012
    17,788
    4,808
    If you want to round it up, as you stated, then you need to use the ceiling function (probably ceil()).

    That still leaves you wil the problem of outputing the result in binary as opposed to base ten. I wouldn't be surprised if MatLab doesn't have a function to take an integer and return a string that is formatted in binary, though it may or may not putting in the group separators (the space every four bits).

    But you can always write such a function. We can help you with the logic.
     
Loading...