How to print result of dec2bin to file

Example:
A = (0:255);
B = dec2bin(A);
fid = fopen('test.txt' , 'w');
fprintf(fid, '%s', B);
fclose(fid);
When you execute the above the fprintf function prints out the first column of the entire B array and then the entire second column, and so on. That is, it doesn't print the entire first binary value and then the entire second binary value and so on.
How do I fix this?
Thanks!

 Respuesta aceptada

Oleg Komarov
Oleg Komarov el 26 de Ag. de 2011
fprintf(fid, [repmat('%c',1,size(B,2)) '\r\n'], B.');

Más respuestas (2)

Jan
Jan el 26 de Ag. de 2011
The output is written columnwise, but DEC2BIN replies the string in row-orientation. Therefore transposing should solve the problem:
fprintf(fid, '%s', transpose(B));
Jonathon
Jonathon el 5 de Sept. de 2011

0 votos

Thanks Oleg!
---
Hi Jan,
The solution you gave worked except for the fact that it just printed out one gigantic long concatenated string of all the binary values as opposed to having each binary value on its own line like Oleg's did.
---
Thanks to both of you for your help! I really appreciate it. :)

1 comentario

Walter Roberson
Walter Roberson el 6 de Sept. de 2011
Use
fprintf(fid, '%s\n', transpose(B));
if you want different lines. Your original code that Jan was going on would have put everything on the same line.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by