How can I save a variable as -v7 by default and switch to -v7.3 only if the variable is too large using SAVE on 64-bit MATLAB 7.12 (R2011a)?

3 views (last 30 days)
I have a script where I am writing variables in the workspace to MAT-files using the following syntax:
X = zeros(100,1);
save('test.mat', '-v7', 'X')
I prefer the -v7 option because the files are smaller and the SAVE command is quicker. However, if I try to save a file that is too large for -v7, I want to be able to switch to -v7.3 automatically. How can I do this?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 7 Apr 2020
Edited: MathWorks Support Team on 7 Apr 2020
You can test for the warning that occurs when you attempt to save a very large variable into a -v7 MAT-file. Here is an example:
X = zeros(30000, 10000); % create a large matrix that is too large to be saved as -v7
lastwarn('') % This will clear any last warnings so there is no confusion
save('test.mat', '-v7', 'X'); % attempt to save the file in -v7 format
[msg,id]=lastwarn('') % This will save the last warning
if strcmp(id,'MATLAB:save:sizeTooBigForMATFile') % compare the id of the warning with the one we are looking for
save('test.mat','-v7.3','X'); % save the variable in a -v7.3 MAT-file
end

More Answers (0)

Products


Release

R2011a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!