Pergunta

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)

Foi útil?

Solução

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)

Outras dicas

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!

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!)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top