Jeongho Yi
Jeongho Yi

Reputation: 1

<MATLAB> How can I save huge data to .txt file?

I made a 865850 by 4464 matrix.

Then I need to save it to a .txt file.

For that, I use fprintf, but I met a hard obstacle....

There are 4464 columns. How can I designate their formatspec?

They are all integers.

Now I know just one way...

fprintf(fid, '%10d %10d.....%10d', Zeros); (4464times..)

Is the only way to save them?

Thank you~!!

clear all; close all;

loop = 1;
Zeros = zeros(15000, 4464);

fileID = fopen('data2.txt','r');

while loop < 4200
    Data = fscanf(fileID, '%d %d %d:%d %d\n', [5, 100000]);
    Data = Data';
    DataA = Data(:,1);
    DataB = Data(:,2);
    DataC = Data(:,3);
    DataD = Data(:,4);
    DataE = Data(:,5);


    for m=1:100000
        r = DataA(m);
        c = ((DataB(m)-1)*24*6 + DataC(m)*6 + DataD(m))+1;
        Zeros(r,c) = DataE(m);            
    end
    for n=1:4464
        Zeros1{n}=Zeros(:, n);
        fileID2 = fopen('result.txt','a');
        fprintf(fileID2, '%10d %10d\n ', Zeros1{1}, Zeros1{2});
    end
   fclose(fileID2);
   loop = loop + 1;

end

Upvotes: 0

Views: 83

Answers (2)

Hello Lemmy
Hello Lemmy

Reputation: 11

You could also just use the built-in MATLAB ASCII save format (instead of printf):

>> foo = magic( 4 )

foo =

16     2     3    13
 5    11    10     8
 9     7     6    12
 4    14    15     1

>> save( 'foo.txt', '-ascii', 'foo' )  

Upvotes: 1

Marcus M&#252;ller
Marcus M&#252;ller

Reputation: 36354

don't use printf with the whole row. Use the CSV export, or iterate over each element of each row and print it isolatedly.

I frequently like to add that for data of this size, textual storage is a bad idea. No one will ever open this in a text editor and think "Oh, this is practical". Everyone will have a bad time carrying around hundreds of megabytes of unnecessary file size. simply use the savemat methods to store the data if you plan to open it in matlab, or use a binary format, for example by just doing fwrite on the data to a file with a sensible binary representation of your numbers.

Upvotes: 2

Related Questions