1
votes

I need to export a Matrix out of Matlab in .txt format and I dont want any commas in between values. Also I need every row in a new line. Ex:

A = [ 1 2 3 4;5 6 7 8 ]

in .txt format I need:

1 2 3 4

5 6 7 8

Thanks.

2

2 Answers

0
votes

Use num2str:

A = [ 1 2 3 4;5 6 7 8 ]
str = num2str(A);

gives

str =
1  2  3  4
5  6  7  8

Then print that string to file using fprintf, or alternatively use diary:

diary('filename.txt')
disp(str)
diary off 

You can also use

save('filename.txt','A','-ascii')
0
votes

Directly fprintf the matrix to a file:

fid = fopen('A.txt','w')
fprintf(fid,[repmat('%g ',1,size(A,2)) '\n'],A)
fclose(fid)