Stephen Bosch
Stephen Bosch

Reputation: 273

MATLAB R2014b saves figure handles without asking

Try this out.

In MATLAB R2014b, create a new function with the following code:

function f1 = fhandle_test(x,y)
f1 = figure;
scatter(x,y)

func = @(x) disp(x);

save('blob.mat','func')
end

Now call the function with some random vectors:

fhandle_test(rand(1,5),rand(1,5))

When I do this, I get this warning message

Warning: Figure is saved in blob.mat. Loading this file recreates and displays the figure and its
contents. Refer to  save for more information. 
> In fhandle_test at 7 

And sure enough, the figure object is saved in the MAT file and the figure is restored when I load the file. As far as I can tell, this happens only when I save a function handle or a cell array of function handles, and only when this happens inside a function.

Now try it again, but this time change the function so the output of figure() is not assigned to a variable:

function fhandle_test(x,y)
figure;
scatter(x,y)

func = @(x) disp(x);

save('blob.mat','func')
end

This time, no warning.

What's going on here, and is there any way to prevent it, apart from deleting the figure handles before saving? I don't want the figure objects saved in the MAT file and I need the handles, my functions are returning them. I'd also prefer to avoid some ugly hack where I have to search the object space and assign the graphics objects to handles after saving.

Upvotes: 1

Views: 124

Answers (1)

Suever
Suever

Reputation: 65430

This is because when you save an anonymous function to a file, the workspace of the anonymous function is also saved to the file. The workspace of the local function is shared with the workspace of the anonymous function, therefore any variable defined within the local function (including the figure handle) is saved to the file as well.

The rationale behind this is that it is perfectly legal for you to use a local variable within your anonymous function.

function myfunc(x)
    y = 2;
    func = @(x)x+y;
    save('test.mat', 'func')
end

In this case, you would need to store y within the file in order to be able to properly execute the anonymous function when it is loaded back. Unfortunately, MATLAB doesn't actually inspect the anonymous function to discern which variables are used so it just saves everything in the local function workspace (which is why you see the behavior you do).

We can verify that this is the case using the functions function to get information on your anonymous function.

function fhandle_test(x,y)
    f1 = figure;
    scatter(x,y)

    func = @(x) disp(x);

    % Get information on workspace of func
    F = functions(func);
    celldisp(F.workspace)

    save('blob.mat','func')
end

This will yield

ans{1} =

    struct with no fields.

ans{2} =

      f1: [1x1 Figure]
       x: 1
       y: 2

You can verify that the figure handle is within the anonymous function workspaces and will therefore be saved.

One potential workaround (if you really need to save an anonymous function) is to place the anonymous function within another function where the only local variables are the ones that you actually want to save to the file.

function fhandle_test(x,y)
    f = figure;
    scatter(x,y)

    otherfunc(x);
end

function otherfunc(x)
    func = @(x)disp(x);
    save('blob.mat', 'func');
end

Upvotes: 5

Related Questions