View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Save MAT files more quickly

4.7 | 8 ratings Rate this file 62 Downloads (last 30 days) File Size: 1.84 KB File ID: #39721 Version: 1.0

Save MAT files more quickly


Tim Holy (view profile)


Saves large numeric arrays more quickly than Matlab's built-in save command

| Watch this File

File Information

When working with large arrays, Matlab's 'save' command can be very slow, presumably because Matlab attempts to use compression. This function provides a much faster alternative, at the cost of larger files. It only improves speed for variables that are plain arrays; elements of structures or cell arrays are not altered.

% First make some data
>> ops = struct('algorithm', 'greedy');
>> A = int32(randi(20, 1000, 1200, 40));
>> B = randn(500, 1800, 60);
% Now save it
>> tic; save /tmp/test ops A B; toc
Elapsed time is 22.980294 seconds.
>> tic; savefast /tmp/test ops A B; toc
Elapsed time is 0.571098 seconds.

Happy saving!

Required Products MATLAB
MATLAB release MATLAB 8.0 (R2012b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (21)
31 Oct 2016 Daniel

Daniel (view profile)

How can we append files to data already saved?

24 Feb 2016 He Qihong

It doesn't work correctly in Matlab2014b. It will makes Matlab2014b crash.

Comment only
06 Jan 2015 Benedikt

Coming back to the complex-number issue discussed by Andre and Raj. A small addition e.g.

saves all complex numbers using the default (slow) command instead of hdf5 without compression. This would have saved me some trouble. Maybe add a comment to the file description as well.

All in all the plugin is very nice.

07 Sep 2014 Tim Holy

Tim Holy (view profile)

@arnold, does it work if you list variables explicitly? It's possible it's just missing a variant that saves all the variables in your workspace.

Comment only
06 Sep 2014 arnold

arnold (view profile)

I don't seem to understand the syntax, it doesn't behave like 'save'.


Does produce a file with the appropriate name but it doesn't seem to contain anything. Loading it does not restore any of the workspace variables.

Comment only
07 Jun 2014 Tim Holy

Tim Holy (view profile)

@zhang, the problem is that '-v6' has some serious limitations, for example it can't store arrays bigger than 2GB. It's also not as portable to other languages, etc; '-v7.3' is based on HDF5, so many different languages can read the files.

Comment only
07 Jun 2014 zhang

zhang (view profile)

This is unnecessary. Use the build-in save(filename, variables,'-v6') is even much faster than this file.

e.g., test showed that:
if not use '-v6', it takes 7.7 seconds for a file.
Using this file, it's 0.73 seconds.
But using the '-v6' option, it's ONLY 0.17 seconds!

Comment only
05 May 2014 Tim Holy

Tim Holy (view profile)

@yaron, off the top of my head I have no idea. What is the error message, and what are version(s) of Matlab on each?

Comment only
05 May 2014 yaron

yaron (view profile)

Great contribution - but I have an issue with using it on a mac. For some reason, while the files load on a PC, the save fast .mat files cause an error on my mac. Any idea for how to fix this?

10 Jan 2014 Tim Holy

Tim Holy (view profile)

Ali, it can only handle certain types, I didn't try to reverse engineer the entire format.

But see the comment from Raj, immediately below yours, about hlp_serialize.

Comment only
10 Jan 2014 Ali

Ali (view profile)

Thank you for providing this code. I tried to use it for fast saving of complex-sparse matrices and it ends with the following error:

Error using hdf5lib2
The HDF5 library encountered an unknown error.
Error in H5D.write (line 72)

do you think it there is a solution to this error or it is not applicable to large complex sparse data format?

Comment only
03 Dec 2013 Raj

Raj (view profile)

Andre..may be your problem might be resolved if you first serialize the data using hlp_serialize( and store using savefast function and later deserialize the data. This worked even when I had complex vector is a structure...definitely improved saving files with marginal increase in memory!

Comment only
03 Dec 2013 Raj

Raj (view profile)

09 Oct 2013 Andre Kuehne

10 Sep 2013 Andre Kuehne

Great submission, however it will not work for complex (real/imaginary) datatypes! The Matlab highlevel hdf5 routines apparently do not support complex datatypes and thus only the real part is saved. This is probably not easily fixed, as you would need to utilize low-level hdf5 functions.

Comment only
11 Jul 2013 Tim Holy

Tim Holy (view profile)

Robin, that's good to know about parfor. Thanks for providing a version that works under such circumstances.

Comment only
11 Jul 2013 Robin

Robin (view profile)

This is great. One problem was that 'evalin' doesn't work properly within a parfor loop. So I made a minor modification to allow 'save -struct' style saving which works fine in parfor:

18 Jun 2013 Tim Holy

Tim Holy (view profile)

Hi Jeremy,

Your performance benefit will depend a lot on your hard drive configuration. In my case I was writing to an SSD RAID. But on my laptop (with a single SSD) I can write in about 2.1 sec, which is still a 10x benefit. What kind of performance benefit are you getting?

Comment only
18 Jun 2013 Jeremy

Jeremy (view profile)

I am not seeing anywhere close to that save time using your A,B,ops sample. I'm running 2012b.

Your example is showing that you saved ~600MB in .57 sec. That seems a bit suspect as it is over 1GB/sec.

Comment only
17 Jun 2013 Robin

Robin (view profile)

01 Jun 2013 Chien-Tung Hsu

Contact us