writing a 16-bit binary file

Hi there,
I am trying to produce a text file of 16-bit binary to test a C program.
I can write to a file, but it writes to the file in scientific notation. I am basically after a file with about 2048 lines of 16-bit ones and zeros or alternatively, just in standard form as in: 32767 -32768 not 3.2767e -4
if that could be put in the form of
0000 0010 1100 1000
kind of data that would be even better. I can only get this far. I am using a sine wave to generate the numbers, and the data only need be approximate, as it is to test a C routine that takes data straight from a circuit.
x = 0:1:1024; y = 32767*sin(x); fid = fopen('data.txt','w'); writebytes(fid, '%5.0d\n',y); fclose(fid);
kind regards Rob

2 Kommentare

Asaad
Asaad am 23 Nov. 2019
can i convert any binary number into 16 bit binary number ? If yes how what is the matlab code for it?
if isa(TheNumber, 'uint8')
output = uint16(TheNumber);
elseif isa(TheNumber, 'int8')
output = typecast( int16(TheNumber), 'uint16');
else
output = typecast(TheNumber, 'uint16');
end
If the original number was more than 16 bits wide then output will be a vector of uint16 . It is not obvious what 16 bit number you would want output if the input was, for example, a double.

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Jan
Jan am 12 Jan. 2014

0 Stimmen

writebytes is thought for mupad objects. fprintf will be better:
fid = fopen('data.txt','w');
fprintf(fid, '%5.0d\n', y);
fclose(fid);
Robert
Robert am 12 Jan. 2014

0 Stimmen

No, this is still giving me the text in the form: 3e+04
I need the data written in the file as either 16 bit binary, or just plain decimal if that is all matlab can do.

5 Kommentare

dpb
dpb am 13 Jan. 2014
Bearbeitet: dpb am 15 Jan. 2014
Matlab can do whatever you want--you just have to be clear in defining it. If it's stream file you want (binary) then as noted earlier,
doc fwrite % is your friend
fid=fopen('file.dat','w');
fwrite(fid,round(y),'integer*2')
If it's a human-viewable text representation of 16-bit digits then
doc dec2bin
and write the strings with fprintf instead.
What would you like the input form for the C side to be--unformatted stream or formatted ASCII is the question.
ADDENDUM/ERRATUM:
Forgot the limitation on dec2bin of only handling unsigned integers -- why TMW hasn't ever fixed this(+) is beyond me, but...
There's at least one fix on File Exchange at
(+) Is there any machine that isn't 2's complement on which Matlab will run?
ADDENDUM 2:
Actually, just came to me--a shorter code-wise way to generate the character string for 2's complement integers including signed ints is
sprintf('%d',bitget(x,1:N))
where N is the length of the integer in bits (16 in your case).
Needs a little help to vectorize but eliminates the if test in the solution in the File Exchange solution and similar implementations on File Exchange. Just a sidebar note--I presume beings as you've apparently never come back you have achieved a solution.
Stefan Oprean
Stefan Oprean am 12 Okt. 2017
Bearbeitet: Stefan Oprean am 12 Okt. 2017
Hi it seems that in my case the data get saturated at 0x7fff, so I cannot write 0xcfff... I also posted a question on stackoverflow
dpb
dpb am 12 Okt. 2017
Bearbeitet: dpb am 12 Okt. 2017
Again you've yet to fully define what it is you're trying to output, specifically. If you want an answer, you've got to define the problem clearly.
But, if it is just writing stream file, there's no limitation on bits written as long as fit in the requested size --
>> val=hex2dec('0cff')
val =
3327
>> fid=fopen('stefan.bin','w');
>> fwrite(fid,val,'integer*2');
>> fid=fclose(fid);
>>
Go look at content with file dump utility to see content--
>> c:\MLR2014b\work > ty stefan.bin
shows --
stefan.bin F1 Help │ Commands: BFGHINPWX │ Col 0 Line 1 50%
0000 0000 FF 0C  .
No problem writing to a file (of course, Windoes is little-endian as shown so order is reversed as in internal storage rather than visual representation.
But, we still don't really know if that's what you're after or something else...show input and expected output of the issue here; don't expect somebody to go somewhere else searching for klews...
dpb
dpb am 12 Okt. 2017
Bearbeitet: dpb am 13 Okt. 2017
I did go read; at least you did clarify there you are trying to write a stream file...the problem is undoubtedly in how you're holding the data internally; Matlab does "saturate" signed integers in some of the dec2hex and like but there's no issue in writing an unsigned int to stream file with fwrite.
I just did the same exercise as above excepting with
>> val=uint16(1:65535);
and verified each and every element is in the file...
We need to see precise code you used that cause the problem to be able to see where you went wrong, specifically.
But, the answer is probably shown by
>> v=int16(0:65535);
>> v(1)
ans =
0
>> v(end)
ans =
32767
>> sum(v==v(end))
ans =
32769
>> v(length(v/2)-4:length(v/2)+4)
Index exceeds matrix dimensions.
>> v(length(v)/2-4:length(v)/2+4)
ans =
32763 32764 32765 32766 32767 32767 32767 32767 32767
>>
OTOH,
>> v=uint16(0:65535);
>> [v(1) v(end)]
ans =
0 65535
>>
The problem is NOT fwrite, it's how you're storing the data internally; fwrite faithfully will output what's in memory.
Walter Roberson
Walter Roberson am 8 Jul. 2021
By the way, these days dec2bin handles negative values.

Melden Sie sich an, um zu kommentieren.

Gefragt:

am 12 Jan. 2014

Kommentiert:

am 8 Jul. 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by