MATLAB: Writing bin data to *.dat file

dlmwriteMATLAB

Hey everybody,
again I couldn't find the right solution and need your wise counsel 😉
I have a binary vector i.e. like this: x = rand(50,1)*7; x = fi(x,false,3,0) x = bin(x);
Now I want to write this to a dat file. I acutally thought I could use dlmwrite('test.dat', x, 'newline', 'pc') but if x = [101 ; 110 ; 001 ; 011 ; …]
the test.dat file looks like this: 1,0,1 1,1,0 0,0,1 0,1,1 . . .
and not like I would like to have it: 101 110 001 011 . . .
Any idea how I can teach dlmwrite to neglect the commas while still using the newline parameter? Should I probably use some other function?
Beste regards and thanks in advance,
Lennart

Best Answer

I don’t have the Fixed Point Designer, so I’m relying on the documentation for bin. The bin function returns a character array, and the dlmwrite function writes only numeric data. So it would seem that you have to convert the string to double-precision numeric, and use the 'precision' name-value pair argument.
See if this does what you want:
x = ['101' ; '110' ; '001' ; '011'];
xn = str2num(x);
dlmwrite('#TEST.txt', xn, 'precision','%03d')
type #TEST.txt
101
110
001
011
You will have to experiment to tweak it to your specifications, but this seems to be as close as dlmwrite can get to doing what you want.