Reputation: 705
I am working on analysis of a large data set using matlab. I would like to be able to run something along the lines of the fprintf command on this matrix, which has about 22000 columns. So, here is what I had in mind so far:
j=22;
for i = 1:j;
fname = fopen(strcat('chr', num2str(i), '.out'), 'r');
A = fscanf(fname, '%d', [1000,inf]);
FIDW = fopen(strcat('chrproc', num2str(i), '.out'), 'w+');
fprintf(FIDW, '%d\t%d\t%d\t%d\t%d\t%d\t\n', B);
end
there are 22 files this size that will be turned into matrices via lines 1-4. However, the tricky part (at least for me) is that fprintf asks you for the FORMAT. Because these files are so large, there is no real way to enter in %d\t.
Perhaps the fgetl command is better? But fgetl does not print to a file, and more importantly, fgetl returns a string, which does not work well for me. Really, something like the fscanf command would be great, except that reads instead of printing...
Thank you very much for your help and advice.
Upvotes: 3
Views: 474
Reputation: 2017
Marsei's answer is correct. However in matlab coder I had to take a different approach (works in regular matlab also):
function printmatrix(matrix)
matrix = matrix';%transpose
[rows, cols] = size(matrix);
format=['%-10g ' 0];
for i=1:cols
for j=1:rows
fprintf(format,matrix(j,i));%Flipped because of matlab
end
fprintf('\n');
end
fprintf('Size of matrix: %g x %g\n', cols, rows);%Backwards because we invert it.
end
See: https://stackoverflow.com/questions/38601352/how-to-print-a-matrix-in-matlab-coder
This also works (but not in matlab coder):
m=magic(5);
[rows cols] = size(m);
x = repmat('%4.0f\t',1,(cols-1));
fprintf([x,'%4.0f\n'],m');
Upvotes: 0
Reputation: 7751
You may use one of the options described in this matlab doc. The possibilities are:
save -ascii
(beware of the scientific notation)dlmwrite
fprintf
as you mentionned, with having format defined as fmt = repmat('%d\t',1,8);
(8 to be replaced by your actual number of columns)Alternatively, you can use the following File Exchange function called saveascii
.
Upvotes: 5