Does anyone here know how to delete a variable from a matlab file? I know that you can add variables to an existing matlab file using the save -append
method, but there's no documentation on how to delete variables from the file.
Before someone says, "just save it", its because I'm saving intermediate processing steps to disk to alleviate memory problems, and in the end there will be almost 10 GB of intermediate data per analysis routine. Thanks!
Interestingly enough, you can use the -append
option with SAVE to effectively erase data from a .mat file. Note this excerpt from the documentation (bold added by me):
For MAT-files,
-append
adds new variables to the file or replaces the saved values of existing variables with values in the workspace.
In other words, if a variable in your .mat file is called A
, you can save over that variable with a new copy of A
(that you've set to []
) using the -append
option. There will still be a variable called A
in the .mat file, but it will be empty and thus reduce the total file size.
Here's an example:
>> A = rand(1000); %# Create a 1000-by-1000 matrix of random values
>> save('savetest.mat','A'); %# Save A to a file
>> whos -file savetest.mat %# Look at the .mat file contents
Name Size Bytes Class Attributes
A 1000x1000 8000000 double
The file size will be about 7.21 MB. Now do this:
>> A = []; %# Set the variable A to empty
>> save('savetest.mat','A','-append'); %# Overwrite A in the file
>> whos -file savetest.mat %# Look at the .mat file contents
Name Size Bytes Class Attributes
A 0x0 0 double
And now the file size will be around 169 bytes. The variable is still in there, but it is empty.