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.