Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
664 views
in Technique[技术] by (71.8m points)

print n*m matrix in matlab

In MATLAB I'm printing a very large matrix this way:

fid = fopen('c:\OUTPUT.txt','wt');
fprintf(fid,'%f',T');
fclose(fid);

But this is not right! I want to print it like this:( between them and at the end of row)

1   2   3
4   5   6
7   8   9
10  11  12

I searched and found If it was 3*3 this was fine:

fprintf(fid,'%f %f %f
',T');

But I in my case size change...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the very simple

fprintf([repmat('%f', 1, size(A, 2)) '
'], A');

You will have one superfluous tab at the end of each line, though:

>> A = magic(5)

A =

    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

>> fprintf([repmat('%f', 1, size(A, 2)) '
'], A')
17.000000   24.000000   1.000000    8.000000    15.000000   % oh, a tab
23.000000   5.000000    7.000000    14.000000   16.000000   % oh, a tab 
4.000000    6.000000    13.000000   20.000000   22.000000   % oh, a tab
10.000000   12.000000   19.000000   21.000000   3.000000    % oh, a tab
11.000000   18.000000   25.000000   2.000000    9.000000    % oh, a tab

To print the output to a file, just use

fprintf(fid, [repmat('%f', 1, size(A, 2)) '
'], A')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...