Publishing .fig files without having them appear on screen - matlab

I have some code that generates a number of MATLAB figures. At the end of my program I want to publish these figures in a report. I have a script, which is passed to publish(), that uses openfig() to include the figures in the document.
This causes these figures to flash up on the screen. This is particularly annoying when I am opening figures inside a loop using a combination of close and snapnow. I've tried making these figures invisible using
openfig(PathToFigure, 'new', 'invisible')
This stops the image appearing on the screen but also stops it appearing in the report.
Is there a way of including .fig files in the report without having them appear on screen?

Open the figure with f=openfig(PathToFigure, 'new', 'invisible'). Then move the figure off-screen by setting its Position property (perhaps to something with negative values for the left and bottom pixels), set its Visible property to on, call snapnow. Delete the figure.

I would actually recommend setting their visibility to 'off' when you create those figures with
f = figure('Visible','off');

Related

Save figures EPS from Matlab P file without display

My function calls code from other functions I did not write those but I know what they do. One of them is .p Matlab file with obfuscated content.
I am doing batch processing of a number of files. I want to write the figures directly to file without display. So I can go through them separately.
Any ideas on how to achieve this.
Thanks!
Script to save matlab figures to a specified directory
The above link works but I still want to avoid displaying the figures at all. Directly print to file. But the work around can simply close all open figures after that loop.
In your script, as soon as you create the figure, set its visibility to off.
For example:
figure(28732);
set(28732,'visible','off'); %Now the figure is not shown
You can now work with the figure, plot, save etc, without the visual clutter or system overhead of displaying it.
If you want ALL of your figures to start without visibility, you can set the default property, as follows:
set(0,'DefaultFigureVisible','off').
This will cause all generated figures to be generated without visibility. (Note, this will be very confusing is you forget that this property is set.)
You should still close the figure as soon as possible in the script, as a part of good memory management.

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?
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.

Dynamic grid of images in MATLAB

I'm working on creating a GUI in matlab using GUIDE. However, i'm not exactly sure how to do the following, and was looking for some tips/advice.
Problem
I want to open a directory and display all the images in that directory in the GUI interface when if it's selected. However, since I will never know exactly how many images there are I am not entirely sure how to do this in the GUI.
Essentially, I want to open the directory and all the images to be displayed in a grid on the GUI similar to that in iphoto.
Current code
Currently, I can open a directory fine, and get all the required information as follows:
directory = uigetdir(pwd, 'Directory Selector');
files = dir(fullfile(directory, '*.jpg'));
strcat(strcat(directory, '/') , files.name) %outputs each file's location
I'm just not sure how to translate this information into the GUI without writing numerous handles.axes1. I understand that since I know this info I could loop over them, but would I not have to create the axes to begin with?
You probably don't want to do this with individual controls - the reason is that MATLAB will have to render each and every one, which will be slow if the directory has a lot of images. Clearly, you can only display a certain number of images on screen at once. You would also have to write your own scrolling code (or some kind of pagination control).
If you have MATLAB > R2008, you can put images in uitable cells using HTML:
% Example for a control with a 'String' property
set(handles.myControl, 'String', '<html><b>Logo</b>: <img src="http://UndocumentedMatlab.com/images/logo_68x60.png"/></html>');
See also this post and this Undocumented MATLAB page.
A different option would be to use the Windows common controls ListView.
A simpler way of doing this would be to have a single image and a listbox of files; an example is here
You can add components to a GUI pprogrammatically. There's more information here.
Each new axes can be added with something like this:
ah = axes('Parent',hObject,'Position',[left bottom width height]);
where left, bottom, width and height define the size and position of the axes. You'll need to change the position for each axes you create and keep track of the axes handles.

How can I export a PNG of a window with multiple subplots in MATLAB?

I have 8 figures and one textbox (a legend listing the values of simulation parameters). They're all plotted in one window using subplot(). It looks nice, but the window pops up too often and distracts me from my other work. I'd like to automatically save these figures as PNG images rather than having the window keep popping up so I can review the plots and figures later.
What I do know:
How to save a single figure as a PNG
How to use subplot to make multiple figures in a single window
How to not display a window with a single figure (one sets the figure's visibility to 'off')
What I don't know:
How to make a multi-figure window not pop up
How to set the size of a multi-figure window (say, 400 px by 600 px)
How to export the entire window to PNG
Any thoughts would be much appreciated. Thanks in advance!
use
set('gcf','visible','off')
to off showing all figures, and
set('gcf','visible,'on')
to turn showing figures on.
to save a figure first assign a name to it:
fig1 = figure(1);
then use saveas:
saveas(fig1,'filename.png','png')
to set figure size, see: Setting graph figure size

Matlab: opening propertyeditor or plotbrowser forces correct textbox annotation; annotation fails without it

I have a generally happily running program that takes files, plots them, spits out a pdf (letter size). I use annotations to put in a title above a set of three subplots, and to use as a footer with file info and date. I would like the title to be at the top of the page, filling up from margin to margin, centered.
I have two ways of running the program: in 'batch' mode and 'interactive' mode. When in 'interactive' mode, I create the figure with a simple figure() command. When in 'batch' mode, I create the figure with figure('visible','off'). Here is my command for making the annotation:
annotation(obj.hFigure(f),'textbox',[0 0.9 1 0.1],...
'String',title,...
'HorizontalAlignment','center',...
'FontSize',18,...
'LineStyle','none',...
'FitBoxToText','off');
Here, "obj.hFigure(f)" is simply a handle to the figure I am currently processing. As you can see, I place the figure near the top of the figure, and make sure that the text runs off the bottom of the box (in case it is larger).
My problem is with margins on the above annotation. In batch mode (no figures showing), I get 10% or so margins on either side of the text, which ruins the layout. In interactive mode (figures show up), I don't get the margins: the text correctly flows from one edge to the other.
I have narrowed down the problem to the following: I can get the correct response to the ps printing in batch mode if I make the figures visible (figure('visible','on')) AND open up
propertyeditor(gcf);
plotbrowser(gcf);
after each figure is plotted. This makes the program take about twice as long (which isn't a huge deal). But what I don't understand is: what do those two commands do that drawnow or refresh don't accomplish?!
I am unsure about your specific case, but when encountering this kind of problem in the past I have had great success by explicitly setting the figure size with:
set(gcf, 'Position', [100 100 300 300])
and then, before printing/saving setting the PaperPositionMode to auto, which seems to force the printed figure to be the same size as the one shown on screen:
set(gcf, 'PaperPositionMode','auto')