Ben B.
Ben B.

Reputation: 97

How to keep formatting of a double array?

2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000
2000    2000    2000       0       0       0       0       0       0       4       0       0    2000    2000
2000    2000    2000       0       0       0       0       0       0       0       0       0    2000    2000
2000    2000    2000       0       0       0       4       3       3       0       3       3    2000    2000
2000    2000    2000       0       4       4       9      44      31       4       0       0    2000    2000
2000    2000    2000       0       4       0      31     277     531      29       0       0    2000    2000
2000    2000    2000       0       0       4       7       0       3      10       0       3    2000    2000
2000    2000    2000       0       0       4       3       0       0       5       0       1    2000    2000
2000    2000    2000       0       0       0       0       0       0       3       1       1    2000    2000
2000    2000    2000       0       0       0       0       3       1       2       1       1    2000    2000
2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000
2000    2000    2000       0       0       0       0       0       0       0       0       0    2000    2000
2000    2000    2000       0       0       0       0       0       0       0       0       0    2000    2000  
2000    2000    2000       0       0       0       4       0       4       0       0       0    2000    2000
2000    2000    2000       0       0       5      27      21       6       0       3       3    2000    2000
2000    2000    2000       0       3      31     274     316      25       1       1       1    2000    2000 
2000    2000    2000       0       0       4       5       4       6       0       0       0    2000    2000
2000    2000    2000       0       0       0       0       0       1       0       0       0    2000    2000
2000    2000    2000       0       0       0       0       0       1       0       0       0    2000    2000
2000    2000    2000       0       0       0       1       0       0       0       0       0    2000    2000
2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000
2000    2000    2000       0       0       0       0       0       0       0       0       0    2000    2000
2000    2000    2000       0       0       0       0       3       0       0       0       0    2000    2000
2000    2000    2000       0       0       0       4       0       3       0       0       0    2000    2000
2000    2000    2000       0       0       3       3      64       6       0       3       0    2000    2000
2000    2000    2000       0       0       3     473     374      30       9       1       0    2000    2000
2000    2000    2000       0       4       3       4       2      10       3       2       0    2000    2000
2000    2000    2000       0       0       0       0       0       0       1       1       0    2000    2000
2000    2000    2000       0       0       0       0       0       0       1       1       0    2000    2000
2000    2000    2000       0       0       0       0       0       0       0       2       1    2000    2000
2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000
2000    2000    2000       0       0       0       0       0       0       0       0       0    2000    2000
2000    2000    2000       0       0       0       0       0       0       0       0       0    2000    2000
2000    2000    2000       0       0       0       3       0       3       0       0       0    2000    2000
2000    2000    2000       0       0       4      18      34       9       0       0       0    2000    2000
2000    2000    2000       0       0       6     251     344      17       2       0       0    2000    2000
2000    2000    2000       0       0       8       2       3       5       0       3       0    2000    2000
2000    2000    2000       0       0       0       0       0       4       0       2       0    2000    2000
2000    2000    2000       0       0       0       0       0       1       0       0       0    2000    2000
2000    2000    2000       0       0       0       0       0       1       0       0       0    2000    2000
2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000    2000

Above is the table that I'm generating in MatLab currently, but when I try saving it, it just sticks it in one long string when i need to keep it in this formatting. Also i need them to be comma separated in this format.

This is the snippet of code that I'm trying to use to save it.

new_fname = ['star_chip' '_' date '.txt'];
path1 = '\\pfile01thn\bbruffey$\My Documents\IDLtoMatlab\star_chips\';
fid = fopen([path1 new_fname], 'w');
fprintf(fid,'%d',star_block);
fclose(fid);

Upvotes: 1

Views: 284

Answers (3)

Ben A.
Ben A.

Reputation: 1039

Here's another possiblity for you to play with. This doesn't get the commas in there, I will take a stab at getting that to work later if you really want them.

This is assuming the matrix you want to save is named A and fid is the file id for the file you've opened to write to.

fid = fopen('filename path here', 'w');
for n = 1:size(A,1)
    fprintf(fid, '%5d', A(n,:))
    fprintf(fid, '\r\n')
end

The %5d' in the fprintf command specifies a field width of 5 for a data type of signed integer. You could replace the d with an i or u if you really wanted to. d and i are the same though. u is just unsigned. There are tons of other modifiers you could use if you wanted to, just take a look at the function in the help file.

Also the second fprintf line is to be used when viewing in notepad. It adds a carriage return and a newline so it bumps the next row down and does so within notepad (which required the carriage return) as well.

Hope that helps some!


A somewhat messy (as in another loop, eww =P ) way to get the comma in that I thought of.

fid = fopen('filename path here', 'w');
for n = 1:size(A,1)
    for m = 1:size(A,2)
        fprintf(fid, '%5d', A(n,m));
        fprintf(fid, ',');
    end
    fprintf(fid, '\r\n');
end

I'm not a big fan of using this route but it does get the job done. If A is really large you're effectively going through every element of it here.


This reflects the comment left by Thor:

fid = fopen('filename path here', 'w');
for n = 1:size(A,1)
    for m = 1:size(A,2)
        fprintf(fid, '%5d,', A(n,m));
    end
    fprintf(fid, '\r\n');
end

Upvotes: 2

Rody Oldenhuis
Rody Oldenhuis

Reputation: 38032

You might want to look into

dlmwrite('output.txt', yourMatrix);

and it's accomplice:

yourMatrix = dlmread('input.txt')

Naturally, there is a bunch more options you might need. Type help dlmwrite or doc dlmwrite for more information.

Upvotes: 1

Thor
Thor

Reputation: 47099

Use the dlmwrite function instead, if you want it stored as an ASCII table use:

dlmwrite(new_fname, int64(star_block), '\t')

Upvotes: 1

Related Questions