There are a lot of reasons why you might want to programmatically define the properties of a figure. Perhaps you want to make a slideshow or video out of a series of figures. Maybe you are producing images for a poster, and you have already defined your layout and want your code to output figures you can drop in without having to resize.
It turns out you can do this with Matlab pretty easily. The first thing to do is download export_fig from the Matlab File Exchange. This function allows you to export Matlab figures to a variety of formats that look much more like the figure displayed on-screen compared to what Matlab’s saveas
function provides, and avoids the automatic resizing that would otherwise botch the next step. To actually change the figure size, you just need to set the position
figure property. The whole process is shown below:
fig = figure ; % create a new figure
plot(x, y, 'o') ; % plot some data
% resize the figure window
set(fig, 'units', 'inches', 'position', [10 10 30 20])
% save the figure using export_fig
export_fig('myfigure.eps')
The position
vector is defined as rect = [left bottom width height]
. The first two elements define the distance from the lower-left corner of the screen to the lower-left corner of the full figure window, while the last two elements define the dimensions of the window. I am not convinced that the units
property actually understands what an inch is, but you can toy around with the values to get your figure to look the way you want; the important thing is that the aspect ratio and figure size are defined consistently. Check out this help file for more info on the “units” property.
Comments
Want to leave a comment? Visit this post's issue page on GitHub (you'll need a GitHub account).