Growing Arrays Using matfile Function

When writing a large number of large values to a MAT-file, the size of the file increases in a nonincremental way. This method of increase is expected. To minimize the number of times the file must grow and ensure optimal performance though, assign initial values to the array prior to populating it with data.

For example, suppose that you have a writable MatFile object.

fileName = 'matFileOfDoubles.mat';
matObj = matfile(fileName);
matObj.Properties.Writable = true;

Define parameters of the values to write. In this case, write one million values, fifty thousand at a time. The values should have a mean of 123.4, and a standard deviation of 56.7.

size = 1000000;
chunk = 50000; 
mean = 123.4;
std = 56.7;

Assign an initial value of zero to the last element in the array prior to populating it with data.,size) = 0;

View the size of the file.

  • On Windows® systems, use dir.

    system('dir matFileOfDoubles.mat');

  • On UNIX® systems, use ls -ls:

    system('ls -ls matFileOfDoubles.mat');

In this case, matFileOfDoubles.mat is less than 5000 bytes. Assigning an initial value to the last element of the array does not create a large file. It does, however, prepare your system for the potentially large size increase of matFileOfDoubles.mat.

Write data to the array, one chunk at a time.

nout = 0;
while(nout < size)
    fprintf('Writing %d of %d\n',nout,size);
    chunkSize = min(chunk,size-nout);
    data = mean + std * randn(1,chunkSize);,(nout+1):(nout+chunkSize)) = data;
    nout = nout + chunkSize;

View the size of the file.

system('dir matFileOfDoubles.mat');

The file size is now larger because the array is populated with data.

See Also

Related Topics