0

I'm trying to draw lines across images in different subplots. The basic idea is to use an annotation, thanks to this question on SO.

Here's a script on Mathworks File Exchange but it's not working on modern versions of MATLAB, and not working on axis image.

Community
  • 1
  • 1
SolessChong
  • 3,370
  • 8
  • 40
  • 67
  • just out of curiosity, why not do it manually using the fig editor? then you can "file>generate code" and add the annotation to your code if needed... – bla Apr 23 '14 at 03:20
  • Good idea. However I want to do it programmatically since there are quite a lot lines and it would be even cooler if I could draw these lines on mouse event. – SolessChong Apr 23 '14 at 04:03
  • so, the question is how to convert coordinates from subplot into figure? if yes,my first attempt would be to check for the units (pixels etc.) for both (axes and figure). next would be getting the position of the axes within the figure... – Lucius II. Apr 23 '14 at 07:27
  • If you follow @natans suggestion, then you would just do File->generate code once and then you can easily create a function etc to re-use it. Is there a reason why that wouldn't work? – kkuilla Apr 23 '14 at 07:49

1 Answers1

0

I find the answer myself.

Refer to this file on Mathworks file exchange.

It calls for a lot of nasty tricky calculations and transformations. Courtesy to Benoît Valley

First, get config of the figure,

% get axes properties
funit=get(get(h_axes,'Parent'),'Units');
% get axes properties
aunit=get(h_axes,'Units');
darm=get(h_axes,'DataAspectRatioMode');
pbarm=get(h_axes,'PlotBoxAspectRatioMode');
dar=get(h_axes,'DataAspectRatio');
pbar=get(h_axes,'PlotBoxAspectRatio');
xlm=get(h_axes,'XLimMode');
ylm=get(h_axes,'YLimMode');
xd=get(h_axes,'XDir');
yd=get(h_axes,'YDir');

% set the right units for h_axes
set(h_axes,'Units',funit);
axesoffsets = get(h_axes,'Position');

x_axislimits = get(h_axes, 'xlim');     %get axes extremeties.
y_axislimits = get(h_axes, 'ylim');     %get axes extremeties.
x_axislength = x_axislimits(2) - x_axislimits(1); %get axes length
y_axislength = y_axislimits(2) - y_axislimits(1); %get axes length

and finally

xfigure = xab+xwb*(xaxes-x_axislimits(1))/x_axislength;

Why I don't use generate code?

Generate code gives me this sort of stuff:

% Create line
annotation(figure1,'line',[0.223214285714286 0.694642857142857],...
    [0.552380952380952 0.630952380952381]);

Which is not related to existing lines, points or figures, not to mention the variables not plotted. So basically I'm looking for ways to get those point positions in that annotation coordinates, which is dealt with in the above code.

If I didn't get you guys right on how to use generate code, plz let me know.

SolessChong
  • 3,370
  • 8
  • 40
  • 67