Regarding "fwrite" in matlab

The following lines are a part of my code.
fid=fopen('M1.txt','w');
fwrite(fid,k,'int16');
fclose(fid);
I would like know how to include a delimiter. I need the .txt file to have 16 bits per line since this data has to be read line by line for further processing.
Kindly help me with this at the earliest.
Thank you

 採用された回答

Titus Edelhofer
Titus Edelhofer 2012 年 3 月 22 日

0 投票

O.K., I give it another try:
x = 42;
fprintf(fid, '%s\n', dec2bin(x, 16));
Is that what you need?
Titus

その他の回答 (1 件)

Titus Edelhofer
Titus Edelhofer 2012 年 3 月 19 日

0 投票

Hi Aparna,
are you sure you want to use fwrite? With fwrite you write binary data (open your M1.txt with an editor that can display HEX data). There is no "line" in a text file usually. Could it be that you want to use fprintf instead?
If this is not the case, you could just use fprintf for the delimiter, i.e., add
fprintf(fid, '\n');
Hope this helps,
Titus

8 件のコメント

Aparna
Aparna 2012 年 3 月 19 日
This file as to be imported into xilinx where a vhdl code will read data line by line.
I did as you suggested but it still does not work.
It provides the data in 1 single line as a continuous stream of bits.
I would be really glad if you could help me out with this.
Titus Edelhofer
Titus Edelhofer 2012 年 3 月 19 日
Hi Aparna,
that's what I meant: I'm pretty sure you don't want the bits but "numbers". Try
k = 42;
fprintf(fid, '%d\n', k);
Does this what you need?
Titus
Geoff
Geoff 2012 年 3 月 20 日
Is the xilinx expecting Windows newlines? In that case you'll have to output CRLF, not just LF:
fprintf(fid, '%d\r\n', k)
Titus Edelhofer
Titus Edelhofer 2012 年 3 月 20 日
Or use
fid=fopen('M1.txt','wt');
(note the 'wt' instead of 'w') to handle this automatically.
Titus
Walter Roberson
Walter Roberson 2012 年 3 月 20 日
Not quite, Titus. Using 'wt' outputs CRLF only if you are running on an MS Windows operating system, which is information we are not given. Geoff's suggestion of \r\n with 'w' is needed if the generating code might be running on something other than MS Windows.
Aparna
Aparna 2012 年 3 月 20 日
Thank you!!
But %d prints it as decimal.. is there any way to print it as a 16 bit values?
i tried using %s: which is a string of characters but it did not work.
The following line is what i used:
fprintf(fid, '%s\r\n', k,'int16')
I need it as a 16 bit binary value which should be imported into Xilinx where it gets read bit by bit. Hence i have to save the file as 0's and 1's.
Asadullah
Asadullah 2012 年 10 月 1 日
編集済み: Walter Roberson 2012 年 10 月 1 日
Walter Roberson
Walter Roberson 2012 年 10 月 1 日
That link leads to this question.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by