Here is a simple example:
Step 1: Create a myCreateFcn.m and a myDeleteFcn.m file.
% myCreateFcn.m
function myCreateFcn(src, eventdata, hObject)
% No need to define anything here.
end
% myDeleteFcn.m
function myDeleteFcn(src, eventdata, hObject)
% No need to define anything here.
end
Step 2: Create a simple and empty GUIDE created GUI. Lets call it "createGUIDEGeneratedFigure". In the createGUIDEGeneratedFigure_OpeningFcn we need to set the root's default figure create/delete functions.
function createGUIDEGeneratedFigure_OpeningFcn(hObject, eventedata, handles, varargin)
handles.output = hObject;
set(0, 'DefaultFigureCreateFcn', {@myCreateFcn, hObject});
set(0, 'DefaultFigureDeleteFcn', {@myDeleteFcn, hObject});
set(hObject, 'Name', 'GUIDE Generated Figure');
guidata(hObject, handles);
end
Step 3: Create a function called createNonGUIDEGeneratedFigure.m file.
% createNonGUIDEGeneratedFigure.m
function fig = createNonGUIDEGeneratedFigure()
set(0, 'DefaultFigureCreateFcn', {@myCreateFcn, []});
set(0, 'DefaultFigureDeleteFcn', {@myDeleteFcn, []});
fig = figure('NumberTitle', 'off', 'Name', 'Non-GUIDE Generated Figure');
end
Step 4: Create a createAndSaveFigure.m file.
% createAndSaveFigure.m
function fig = createAndSaveFigure(fileName)
fig = figure('NumberTitle', 'off', 'Name', 'Saved Figure');
hold on;
x = -10:10;
plot(x, x.^2, '.b');
plot(x, x.^3, '.r');
saveas(fig, filename, 'fig');
end
Step 5: Create a myReset.m file for resetting the root's default functions.
% myReset.m
function myReset()
set(0, 'DefaultFigureCreateFcn', []);
set(0, 'DefaultFigureDeleteFcn', []);
end
Step 6: Lets test a happy path... (via the command window)
createNonGUIDEGeneratedFigure();
createAndSaveFigure('Problem_Free_Figure.fig');
Step 7: Reset (via the command window)
myReset();
Step 8: Here comes the good stuff! (via the command window)
createGUIDEGeneratedFigure();
createAndSaveFigure('Problematic_Figure.fig');
Step 9: Reset (via the command window)
myReset();
Now examine your new .fig files. Notice the difference in size. Now open them and you will see what my problem is.