1

Suppose that I have 2 figures in MATLAB both of which plot data of size (512x512), however one figure is being plotted by an external program which is sets the axis parameters. The other is being plotted by me (using imagesc). Currently the figures, or rather, the axes are different sizes and my question is, how do I make them equal?. The reason for my question, is that I would like to export them to pdf format for inclusion in a latex document, and I would like to have them be the same size without further processing.

Thanks in Advance, N

Edit: link to figures

figure 1: (big)

link to smaller figure (i.e. the one whose properties I would like to copy and apply to figure 1)

2
  • 1
    Perhaps you can link to examples of the two plots?
    – Dan
    Sep 25, 2013 at 11:05
  • Even though the external program defines the initial size of the axes for one figure, you should be able to homogenize the two axes afterwards. Just save the handles and set the size properties to be the same?
    – Bonlenfum
    Sep 25, 2013 at 11:32

3 Answers 3

1

For this purpose use linkaxes():

% Load some data included with MATLAB
load clown

% Plot a histogram in the first subplot
figure
ax(1) = subplot(211);
hist(X(:),100)

% Create second subplot
ax(2) = subplot(212);

Now link the axes of the two subplots:

linkaxes(ax)

By plotting on the second subplot, the first one will adapt

imagesc(X)

First, you have the following: enter image description here

Then:

enter image description here

Extending the example to images only:

load clown
figure
imagesc(X)
h(1) = gca;

I = imread('eight.tif');
figure
imagesc(I)
h(2) = gca;

Note that the configurations of the the first handle prevail:

linkaxes(h)
10
  • +1 very nice fcn, didnt know about it! is this a new function?
    – Lucius II.
    Sep 25, 2013 at 13:29
  • @LuciusDomitiusAhenobarbus The function has been around for a while, since R14, mathworks.co.uk/help/releases/R14/techdoc/ref/linkaxes.html
    – Oleg
    Sep 25, 2013 at 13:45
  • -> Access to archived documentation is a benefit for licensed users of MathWorks products. Thanks for the information, but I usually am not logged in with a mathworks account :)
    – Lucius II.
    Sep 25, 2013 at 13:46
  • Unfortunately (probably due to my inexperience), I wasn't able to adapt what you recommended to my problem. I have 2 separate figures and because I would like to export them separately, using 'subplot' doesn't really fit. However the function linkaxes does look promising. Based on what I posted below, perhaps there is a more elegant solution to be found with linkaxes?
    – Neil
    Sep 25, 2013 at 14:34
  • @neil You need to store the handles from the axes somewhere (similarly to what I do with subplot) and then call linkaxes on those handles.
    – Oleg
    Sep 25, 2013 at 15:10
1

1.Get the handle of your figure and the axes, like this:

 %perhaps the easiest way, if you have just this one figure:
 myFigHandle=gcf;
 myAxHandle=gca;
 %if not possible, you have to search for the handles:
 myFigHandle=findobj('PropertyName',PropertyValue,...)
 %you have to know some property to identify it of course...
 %same for the axes!

2.Set the properties, like this:

%set units to pixels (or whatever you prefer to make it easier to compare to the other plot)
set(myFigHandle, 'Units','pixels')
set(myAxHandle, 'Units','pixels')
%set the size:
set(myFigHandle,'Position',[x_0 y_0 width height]) %coordinates on screen!
%set the size of the axes:
set(myAxHandle,'Position',[x_0 y_0 width height])  %coordinates within the figure!
0
0

Ok, based on the answer of @Lucius Domitius Ahenoba here is what I came up with:

hgload('fig1.fig'); % figure whose axis properties I would like to copy
hgload('fig2.fig');
figHandles = get(0,'Children');
figHandles = sort(figHandles,1); 
ax(1) = findobj(figHandles(1),'type','axes','-not','Tag','legend','-not','Tag','Colorbar');
ax(2) = findobj(figHandles(2),'type','axes','-not','Tag','legend','-not','Tag','Colorbar');

screen_pos1 = get(figHandles(1),'Position');
axis_pos1 = get(ax(1),'Position');

set(figHandles(2),'Position',screen_pos1);
set(ax(2),'Position',axis_pos1);

This is the 'before' result: enter image description here

and this is the 'after' result: enter image description here

Almost correct, except that the aspect ratios are still off. Does anybody know how to equalize everything related to the axes? (I realize that I'm not supposed to ask questions when posting answers, however adding the above as a comment was proving a little unwieldy!)

2
  • did you check, that the units are the same for both? you then should debug it, checking that the handles are the rigth one
    – Lucius II.
    Sep 25, 2013 at 15:00
  • @LuciusDomitiusAhenobarbus Since my code modified figure 2 (on the right above), I'm assuming that the handles are correct (?). The units for both axes are also the same.
    – Neil
    Sep 25, 2013 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.