Erik
Erik

Reputation: 4563

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

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 choose not to display them by making the default figure invisible with set(0,'DefaultFigureVisible','off'). This sets the 'Visible' property of any new figure to 'off'. This way I can create, edit, save, etc., figures without the need to draw them, which can be taxing on CPU, GPU and their memories. I save the figures as .fig files using the saveas(handle,'filename.fig') command. This also saves the 'Visible' property, which is a problem when I do want to open the figure (e.g. by double-clicking the file in my Windows Explorer). It loads the figure, but it doesn't display it because its 'Visible' property is set to 'off'.

I want all .fig files to be saved with the property set to 'on', but how can I achieve this without displaying (= taxing) the figures? The moment I use set(handle,'Visible','on'), the figure is drawn.

So basically, I want to edit the file on a lower level than when it's loaded as a figure in MATLAB.

I think it could be done as follows, but I do not know exactly how to achieve it. One can load the .fig's data as if it were a .mat file using s=load('filename.fig','-mat');. This loads a structure s containing some fields that contain all the figure's data, properties, etc. Now, the figure handle must be found in this unkown structure and the 'Visible' property that goes along with the handle edited.

Can this be done without the figure being drawn?

I tried, but haven't succeeded, using fopen, fread and their friends.

Does anybody know how to do what I want to do?

Upvotes: 4

Views: 4194

Answers (1)

Erik
Erik

Reputation: 4563

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.

Upvotes: 4

Related Questions