0

I want to create an input file with random 8 bit binary number for VHDL testbench using Matlab.

%create random 10 decimal number
a = randi ( [0 255], 10 , 1 ); 
display(a);
%convert to 8bit binary
a=dec2bin(a,8);
display(a); 
%write to textfile
fid=fopen('eventsIn.txt','wt'); 
fprintf(fid,'%s\n',a); 
fclose(fid);

how i want to make sure that the output become

1010100
1100100
0010100
0111101
0000110

instead of this format?

10101001100100001010001111010000110010011110111000101011101101101110100100100010
3
  • Did you open it with note pad or notepad++? Or anyother depending on OS. Commented Nov 29, 2013 at 13:40
  • I'm using windows and have tried both notepad and notepad++..but still give a single line output Commented Nov 29, 2013 at 14:00
  • possible duplicate of writing binary values into file in MATLAB Commented Nov 29, 2013 at 14:33

3 Answers 3

0

You can do it with your approach, only changing two things:

  1. If each row of a is a number which should be written in a different line, you should transpose a before feeding it to fprintf. That's because fprintf(fid,'%s\n',x) reads first column of x, then second column etc.

  2. Add a carriage return (ASCII code 13) or line feed (ASCII code 10) or both at the end of each column of a.' (that is, in a new row or two new rows). And as a side result you don't need the \n in the format string of fprintf.

So, use

fprintf(fid, '%s', [ a.'; repmat(char(13), 1, size(a,1)) ] ) %// CR only

or

fprintf(fid, '%s', [a.'; repmat(char([10; 13]), 1, size(a,1))] ) %// LF and CR
Sign up to request clarification or add additional context in comments.

3 Comments

Hm, but this way you end each line with a CR, and at the very end you have an additional LF?!
@A.Donda Thanks, now I see what you mean. I have removed \n from the format string
@khairul Please consider marking one of the answers as "accepted", if it does what you want
0

EDIT

First try this:

dlmwrite('output.txt', b, '')

What you currently seem to do is writing all data and then appending a newline.

There are several ways to deal with this. This is a simple one:

Do your writing in a for loop over the lines and append a newline after each line.

If that still doesn't work you can try using \r\n instead of \n.

Comments

0

Your a is a two-dimensional character array, that you would like to be interpreted as a sequence of strings. However, fprintf(fid,'%s\n',a) just handles it as one big string. As Dennis Jaheruddin writes, there are several ways to achieve this. I think this one's pretty simple:

Convert the two-dimensional character array into a cell array of strings:

a = cellstr(a);

Feed it to fprintf as an (implicit) sequence of arguments:

fprintf('%s\n', a{:})

This way, you don't need to program a loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.