Don’t save anonymous Matlab functions

Bug of the day: if you use the save function inside a function, and one of the things you’re saving is an anonymous function, then all the variables in the scope of the (non-anonymous) function will be saved along in the .mat file. So for example:

function funtest
    X = randn(5000);
    a = @(x) x+1;
    save test a

If you look at test.mat you will find that the file is > 150MB. However if you remove the X line it’s less than 1 KB. So X must be saved despite your instructions to the contrary.

Leave a comment

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s