Writing to matfile in a loop
Show older comments
Hello,
I am working on a project involving processing an hour of sampled data, where the data is sampled at several megasamples per second. A single data set can come in at 100 GB+.
In order to determine if my data processing is producing high quality measurements, I want to simulate similar data, and process it. My preference is to generate the data all at once, then process it all at once. Thus, I have a need to generate vectors larger than what my PC can hold in RAM.
I am attempting to generate my large data set one second at a time, then store each second of data to a file. I am doing this in a for loop using the matfile class that is included in R7.14 (2012a), and perhaps earlier.
However, the files I am generating are larger, indeed, much larger, than they should be. My issue can be demonstrated with the following snippet. At the end, there are two files on disk, the first 34208152 bytes, the second 776065 bytes. However, there are two vectors in memory, each 100,000x1.
Why is the file written in pieces 44x larger than the file written in one step?
If there is not a fix for this, do you have any suggestions on how I can generate and store a variable much larger than the amount of RAM available on my PC?
Thank you,
Andrew
% Generate Data that is cause for question
testFile = matfile('simData1','Writable',true);
testFile.y1 = [];
filePtr = 1;
for itr = 1:100
data = randn(1000,1);
testFile.y1(filePtr:filePtr+1000-1,1) = data;
filePtr = filePtr + 1000;
end%for
data = randn(1e5,1);
testFile2 = matfile('simData2','Writable',true);
testFile2.y2 = data;
% Show that files are different size on disk
clear
details = dir;
for itr = 1:length(details)
if strcmp(details(itr).name,'simData1.mat');
fileIdx1 = itr;
elseif strcmp(details(itr).name,'simData2.mat');
fileIdx2 = itr;
end%if
end%for
details(fileIdx1).bytes
details(fileIdx2).bytes
% show that variables are same size in memory
load('simData1')
load('simData2')
Edit: Note that preallocation of testFile.y1, (e.g. testFile.y1 = zeros(1e5,1);) does not resolve the issue.
Accepted Answer
More Answers (0)
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!