How to edit property of figure saved in .fig file without displaying it

后端 未结 1 1981
青春惊慌失措
青春惊慌失措 2021-01-14 01:22

I want to edit a certain property of MATLAB figures saved as .fig (MATLAB\'s default format) files.

I create a lot of graphics-intensive figures in a script, so I ch

1条回答
  •  囚心锁ツ
    2021-01-14 01:41

    I base my solution on the thread from the url posted by user4506754: http://www.mathworks.com/matlabcentral/newsreader/view_thread/306249 There, Jesse Hopkins posts (post 15) that you can edit a property 'ResizeFcn' to execute a function when MATLAB creates a figure. This doesn't work on my MATLAB installation, but lead me to look into the different functions you can attach to a figure in its properties. This page documents all figure properties: http://mathworks.com/help/matlab/ref/figure-properties.html. There I found the 'CreateFcn' property. Its description contains:

    This property specifies a callback function to execute when MATLAB creates the figure. MATLAB initializes all figure property values before executing the CreateFcn callback.

    It means that the figure is loaded with its properties, including the 'Visible' property being 'off' and then 'CreateFcn' is called.

    Setting 'CreateFcn' to make the figure visible then solves my problem.

    set(gcf,'CreateFcn','set(gcf,''Visible'',''on'')')
    

    An example:

    ezplot(@sin) % draw a simple figure containing a sine wave, title, etc.
    set(gcf,'Visible','off','CreateFcn','set(gcf,''Visible'',''on'')' % this disables the figure and set the 'CreateFcn' property simultaneously
    saveas(gcf,'sin.fig') % save the figure in the current folder as a .fig file
    close % closes current figure
    

    Now go to the current folder in your Explorer and double-click the sin.fig file. It makes MATLAB load it and, poof, the figure is drawn.

    Solution found.

    This doesn't edit the .fig file, as I originally asked (as a solution), but it is an alternative solution to the original problem. Now I can create and save figures without them being visible, but draw the figures the moment they're loaded by MATLAB.

    0 讨论(0)
提交回复
热议问题