2
votes

I am trying to get the location of axis TickValues from a MATLAB figure. For example, I have a figure as follows:

MATLAB Figure

I am trying to find the location of axis TickValues after I save the figure as an image (shown in figure) [Note: The bounding boxes are handcrafted. Ignore any error]

axis TickValue Location

Here is the code I generated so far:

h = plot(1:10);
hFrame = getframe(h.Parent.Parent);
hImage = hFrame.cdata;
set(h.Parent,'Units','pixel');

I am trying to get bounding boxes for x-axis TickValues and y-axis TickValues on hImage from the position of h.Parent.

Let me know if the question is not clear yet. I will edit to make it clearer.

1
What are you actually trying to accomplish? This seems like a very round-about way of doing it.Suever
I am trying to extract those regions in the image obtained from the handle(which needs to be in pixels) as we know images are usually in pixels. Those extracted regions will be used in future for some other purpose. Extraction in the image from the figure handle information is the key. I don't understand how it is a round-about way of doing it.user7410580
Why not just use getframe(h.Parent) to save just the part that doesn't include the labels? You probably don't need to save to an image though if you just want the locations of the axes. Just use h.Parent.Position to get that.Suever
I don't think you understood the question then. I am trying to get the location of each axis from the figure handle itself; not the image. The locations obtained from figure handle will be placed in the image as shown above.user7410580
However, you have a point on the position? The positions in the image include for both x-axis and y-axis together along with the figure. I want to separate them out. That's the idea of the question.user7410580

1 Answers

1
votes

As @suever suggested in comments, the Position property of the axes will be the simplest way to do that:

h = plot(1:10);
pos = h.Parent.Position; % get the axes position
m = 3; % width and hight multiplyer
xth = h.Parent.XAxis.TickLength(2); % get x-axis tick hight
dimx = [pos(1)-xth pos(2)-xth*m pos(3)+xth*2 xth*m];
annotation('rectangle',dimx,'color','r') % for demonstration
yth = h.Parent.YAxis.TickLength(2); % get y-axis tick hight
dimy = [pos(1)-yth*(m-1) pos(2)-yth yth*(m-1) pos(4)+yth*2];
annotation('rectangle',dimy,'color','r') % for demonstration

Demonstration:

axis_position

and you can change m to be sure that all the tick values are within the bounding box.


EDIT:

Option 2:

op2

Option 3:

op3