How can I export a PNG of a window with multiple subplots in MATLAB? - 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

Related

Matlab: Export a figure made with myaa

I am trying to get better quality phase plots of complex functions made with the Complex Function Explorer of E. Wegert CFE. For this purpose I apply the Matlab anti-aliasing function myaa.m to the phase plots that are made with the CFEGUI.m. For the example screenshot of the result window below I used the setting myaa([8 8]) in the Matlab command window which means that the supersampling enlarge the figure 8x and then downscale it to 1/8 to get the original h x w.
As one can see the window of the figure has no operation icons or menu options for save or print. My question is what to do with such a figure (beside making screenshots)? Can I somehow use export-fig to save such a figure or load it in an image array and if yes, how?
It is also possible to use the setting myaa([8 1]) which results in a very large figure window that is larger than my screen (and has the unpleasant attribute that it can not be moved). It would be even better if such a whole figure could be saved (not only the visible part).
You can use getframe to grab current figure information in pixels.
However, while I do not know how this affects you, notice that MATLAB versions above 2014b (included) have already anti-aliasing. The code you linked seem to be from 2008, I am not 100% sure if it has become obsolete now.

Matlab figure contents lost while saving as eps/pdf

I have tried most of the usually used options (print(figr, '-depsc', 'cross_corr.eps');) to save a matlab figure as eps/pdf but each time I do it, the figure contents are saved partially and perhaps one quarter of it is lost.
I have shared the figure here: http://ge.tt/2ZrsdD02/v/0?c
Using the options such as the following save it completely but I prefer it save it directly inti eps/pdf:
print(figr, '-dpng', 'cross_corr.png');
The problem seems to be that you have very small values in the 'PaperPosition' property of the figure. Try changing them to the default ones,
set(figr, 'PaperPosition', [0.634517 6.34517 20.3046 15.2284])
and then apply your print(figr, '-depsc', 'cross_corr.eps').
I attach results on my computer without and with 'PaperPosition' correction (using GSview with bounding box showed):
Also, consider reducing font size to avoid overlapped text:
set(findobj('type','text'), 'Fontsize', 5)
If I first enlarge the figure size (by using the middle button on the top right), and then save it as eps, I get this: http://ge.tt/1Pv8YE02/v/0
The quality is very nice as compared to all other options and the content are also ok.
Its not possible to automate it through script?...

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.

Publishing .fig files without having them appear on screen

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');

MatLab: displaying figures side by side

As when you use subplot to display plots side by side in the same window, I would like a similar function that can do the same for multiple figures. This works:
subplot(2,2,1)
and I am looking for something like this:
subfigure(2,2,1)
Does a build-in function with this advantage exist?
You could write one such function yourself. The idea is to get the screen size:
get(0,'ScreenSize')
then divide it up into sub-regions according to the input parameters. You would also need to account for margins in-between.
Another idea:
create an invisible figure (preferably same aspect ratio as the screen)
call subplot inside it
capture the position of the created axis
delete figure
scale the position captured to fit the screen size, and use it to create the actual figures.