I have a matrix cube which I load in my program to read data from. The size of this .mat file is 2.8 GB. I am not being able to load it with the error of 'running out of memory'. Is there a way to fix this?
You can use the matfile class to work on ranges within variables inside MatLab files. See
Load and save parts of variables in MAT-files
Here's some additional discussion that discloses that this feature is new with R2011b.
If the size of the data exceeds the available memory on your machine, then you are in trouble - this is unavoidable. However, if you only want certain variables inside the .mat file you can try to load just those variables using the
load(filename, variables)
version of the load function. It really depends on the contents of your .mat file. If the file is 2.8GB and you need ALL of the variables in the file and your machine does not have enough memory to cope, your only option is to buy more RAM.
EDIT Apparently this answer is incorrect if you are running R2011b and above as explained in the answer of Ben Voight
Related
I'm running a short code to open one by one a list of files and saving back only one of the variables contained in the files. The process seems to me much slower than I expected and getting slower with time, I don't fully understand why and how I could make it run faster. I always struggle with optimization. I'd appreciate if you have suggestions.
The code is the following (the ... substitute the actual path just for example):
main_dir=dir(strcat('\\storage2-...\Raw\DAQ5\'));
filename={};
for m=7:size(main_dir,1)
m
second_dir=dir([main_dir(m).folder '\' main_dir(m).name '\*.mat']);
for mm=1:numel(second_dir)
filename{end+1}=[second_dir(mm).folder '\' second_dir(mm).name];
for mmm=1:numel(filename)
namefile=sprintf(second_dir(mm,1).name);
load(string(filename(1,mmm)));
save(['\\storage2-...\DAQ5\Ch1_',namefile(end-18:end-4),'.mat'], 'Ch_1_y')
end
end
end
The original file is about 17 MB and once the single variable is saved it is about 6 MB in size.
The Matlab load function takes an optional additional argument to specify just a selected variable to read from the input file.
s = load('path/to/file.mat', 'Ch_1_y');
That way you don't have to spend time loading in all the other variables from those input .mat files that you're just going to immediately throw away.
And using save to save MAT-files over SMB shares can be slow. You might want to call save to write it to a temporary local file first, and then copy the completed file to the final destination. Sounds like more I/O, but it can actually be a net win, depending on your particular system and network. Measure it both ways to see if it's a win in your particular situation.
I can load a matrix from text file:
load mydata.txt
The problem is my matrix file is about 250Mb and after several such loads I have no memory to work with next files.
How could unload it and free resources for further use?
Use clear, or clearvars. By default, MATLAB will create a variable called mydata as a result of your statement, so
clear mydata
Find the variables in your workspace that contain the large data sets and in your script or from the console type
clear whateverVariableName
To clear all memory use
clear all
You can even right-click individual variables in he workspace editor and delete them using the IDE if you wish.
What you have to do clear mydata and then issue pack . The first command says to Matlab that the reference to the memory held for mydata is not needed anymore. The second command instruct the Matlab to free unused memory. If you don't issue pack, then memory will be deallocated when the Matlab memory manager decides to.
I have a big array (1024x1024x360) and I want to save it to a mat file. When I just try
A=rand(1024,1024,360)
save('filename.mat','A');
The variable is created in the workspace, the file is being created, but it remains empty...
I'm using Matlab 2012a on Win7-64 machine, Why is that happening?
Earlier versions of Matlab couldn't save variables larger than 2 GB. Your default save file format may be set to an older type even on newer versions of Matlab; my own install of R2013a seems to have come preset to v7, which won't save anything that big. You have two choices: either specify the format for this file using an extra flag:
save('filename.mat','A','-v7.3');
or change the default for all save files by running preferences and looking in the MAT-files area under General.
I can load a matrix from text file:
load mydata.txt
The problem is my matrix file is about 250Mb and after several such loads I have no memory to work with next files.
How could unload it and free resources for further use?
Use clear, or clearvars. By default, MATLAB will create a variable called mydata as a result of your statement, so
clear mydata
Find the variables in your workspace that contain the large data sets and in your script or from the console type
clear whateverVariableName
To clear all memory use
clear all
You can even right-click individual variables in he workspace editor and delete them using the IDE if you wish.
What you have to do clear mydata and then issue pack . The first command says to Matlab that the reference to the memory held for mydata is not needed anymore. The second command instruct the Matlab to free unused memory. If you don't issue pack, then memory will be deallocated when the Matlab memory manager decides to.
I was trying to save a matrix into a mat file, but the Matlab returns the following messages:
Warning: Variable 'listmatrix' cannot be saved to a MAT-file whose version is older than 7.3.
To save this variable, use the -v7.3 switch.
Skipping...
What does it mean for "use the -v7.3 switch"?
Should I use
save testresult.mat -v7.3 listmatrix
or sth else?
Hi i thought I’d reply to this thread as I’ve been trying to figure out how to save a large (>2 GB) .mat file in matlab v7 (v7.1.0.183) (R14) and finally found a solution.
If you try to use the save command you will get the following error:
save('test.mat', 'data');
Warning: Variable 'data' cannot be saved to a MAT-file because its
storage requirements exceed 2^31 bytes. This limitation will be
addressed in a future release. Consider storing this variable in HDF5
file format (see HDF5WRITE). Skipping...
The solution is to write a HDF5 file instead:
hdf5write('test.hdf5', '/dataset1', data);
You can then read the data back into matlab using:
hdf5read('test.hdf5', '/dataset1');
A quick google search says yes. Try
save -v7.3 testresult.mat listmatrix
How big is your object? (Do whos listmatrix)
You could potentially save memory by using different data type such as uint8.
http://www.mathworks.ch/matlabcentral/newsreader/view_thread/243327
http://www.mathworks.de/matlabcentral/newsreader/view_thread/307845