This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

File Size Increases Unexpectedly When Growing Array

This example shows how to prevent an array from growing when writing one million double-precision values to a file, by assigning initial values to the array.

Construct a object for writing.

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');

matFileOfDoubles.mat is less than 5000 bytes. Assigning an initial value to the last element of the array does not create a large file.

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 now because the array is populated with data.

Was this topic helpful?