Binary in Matlab

Thread Starter


Joined Jan 12, 2014
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);

but it writes it to the file in the format:

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


Joined Mar 31, 2012
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.