MATLAB callback when saving figure file

436 views Asked by At

I need to check programmaticall whether a fig (guide) file was saved or not, so bascially I need a callback when:

myfigure_SavedCallback(hObject, varargin)

I didn't find anything online, so I guess it is not possible, can someone verify this?

SOLVED:

So I just found out how to do this by myself. In the guide editor you can open the Toolbar Editor and change all the icon-buttons a gui usually has. There is somewhere the field Clicked Callback. You can enter the regular callback-formalism, e.g.

mygui('uipushtool2_ClickedCallback',hObject,eventdata,guidata(hObject))

And add this callback to the code. In that particular callback, a UD-flag for saved/unsaved can be handled if necessary.

3

There are 3 answers

6
SamuelNLP On

To check if the image was saved manually:

You can check if the Filename field of the figure is empty or not. As soon as the figure is saved it get the path of the saved figure.

Run the code and save the figure manually yourself.

clc
close all
clear

img = imread('cameraman.tif');

h = figure(1);
imshow(img);

while(isempty(h.FileName))
    clc
    disp('not saved yet');
    pause(0.3)
end

disp('saved in:');
disp(h.FileName);

this will give you:

not saved yet
saved in:
C:\Users\samuel\Desktop\fig.fig
>> 
2
Sam Roberts On

No, as far as I know there is no callback for figures (or other HG components) that executes when they are saved.

However, I think you might be able to work around this. You can create an object of your own that executes code when it is saved - something like this:

classdef mytest < handle    
    methods         
        function sobj = saveobj(obj)
            sobj = obj;
            disp('saved') % Put your code here
        end        
    end    
end

Then you can create one of these objects, and store it in, for example, the UserData property of the figure. When the figure gets saved, so does the object, and whatever code you would like will get executed.

I imagine that there may be some ways in which the above workaround might be defeated by various things that the user could do. But with some care, it might be enough for your needs.

0
rst On

So I just found out how to do this by myself. In the guide editor you can open the Toolbar Editor and change all the icon-buttons a gui usually has. There is somewhere the field Clicked Callback. You can enter the regular callback-formalism, e.g.

mygui('uipushtool2_ClickedCallback',hObject,eventdata,guidata(hObject))

And add this callback to the code. In that particular callback, a UD-flag for saved/unsaved can be handled if necessary.