Matlab: convert global coordinates to figure coordinates - matlab

If I get coordinates via
coords = get(0,'PointerLocation');
How can I convert them into points gotten via ginput?
i.e, I would like to get the same values from
coords = get(0,'PointerLocation');
coords=someConversion(coords);
As I would have gotten by calling
coords=ginput(1);
And clicking inside the figure in the same spot as the mouse was in the previous bit of code.

Here's an example of how you can do this conversion...
Let's say you have a figure, and that figure contains an axes object with handle hAxes. Using the function ginput would allow you to select points within the axes. To get an equivalent set of points from get(0, 'PointerLocation'), which gives coordinates in relation to the screen, you have to account for the figure position, axes position, axes width/height, and axes limits.
Doing this is tricky because you want to have the position measures in the same units. If you want to compute everything in units of pixels, this means you'd have to set the 'Units' properties of the objects to 'pixels', get the positions, then set the 'Units' properties back to what they previously were. I usually make my own function get_in_units to do this part:
function value = get_in_units(hObject, propName, unitType)
oldUnits = get(hObject, 'Units'); % Get the current units for hObject
set(hObject, 'Units', unitType); % Set the units to unitType
value = get(hObject, propName); % Get the propName property of hObject
set(hObject, 'Units', oldUnits); % Restore the previous units
end
Using the above function, you can make another function get_coords which gets the screen coordinates and converts them to axes coordinates:
function coords = get_coords(hAxes)
% Get the screen coordinates:
coords = get_in_units(0, 'PointerLocation', 'pixels');
% Get the figure position, axes position, and axes limits:
hFigure = get(hAxes, 'Parent');
figurePos = get_in_units(hFigure, 'Position', 'pixels');
axesPos = get_in_units(hAxes, 'Position', 'pixels');
axesLimits = [get(hAxes, 'XLim').' get(hAxes, 'YLim').'];
% Compute an offset and scaling for coords:
offset = figurePos(1:2)+axesPos(1:2);
axesScale = diff(axesLimits)./axesPos(3:4);
% Apply the offsets and scaling:
coords = (coords-offset).*axesScale+axesLimits(1, :);
end
The resulting coords should be close to those you would get from using ginput. Note that if the axes object is nested within any uipanel objects in the figure, you will have to account for the panel positions as well.
Example:
To illustrate the behavior of the above code, here's a neat little example. After creating the above functions, create this third function:
function axes_coord_motion_fcn(src, event, hAxes)
coords = get_coords(hAxes); % Get the axes coordinates
plot(hAxes, coords(1), coords(2), 'r*'); % Plot a red asterisk
end
Then run the following code:
hFigure = figure; % Create a figure window
hAxes = axes; % Create an axes in that figure
axis([0 1 0 1]); % Fix the axes limits to span from 0 to 1 for x and y
hold on; % Add new plots to the existing axes
set(hFigure, 'WindowButtonMotionFcn', ... % Set the WindowButtonMotionFcn so
{#axes_coord_motion_fcn, hAxes}); % that the given function is called
% for every mouse movement
And when you move the mouse pointer over the figure axes, you should see a trail of red asterisks being plotted behind it, like so:

You can get the figure's position using getpixelposition(gcf) and then subtract the first 2 elements (x,y of the lower-left corner) from the PointerLocation to get the relative figure location.
For more complex transformation (e.g., relative to some internal panel or axes) you may possibly need to recursively fetch the relative positions of the sub-components. Look at pixelposition.m or moveptr.m for some examples.

Related

MATLAB: issue with putting x and y labels on each side of a plot

Based code from here, I wrote a function which plots a figure and puts x-axis labels on both the top and bottom of the figure, as well as y-axis labels on the left and right side. My problem is that I need to run the code multiple times and each time the labels get written over, and for some reason the y-axis labels get overwritten in a weird way as shown,
first run:
second run:
The following is an mwe:
% sample data, plot
x=[1:168];
y=x;
plot(x, y, 'r', 'LineWidth', 1);
set(gca, 'XTick', [], 'YTick', []);
% set left yaxis label
ylabel(directions{1,1},'Rotation',-360);
% Adjust position - this seems to be what's causing the issue!
ylabelh = get(gca,'YLabel');
rpos = get(ylabelh,'Position');
set(ylabelh,'Position',rpos + [1.5*rpos(1) 0 0])
% do stuff... as per link above
axesPosition = get(gca,'Position');
hNewAxes = axes('Position',axesPosition,... %# Place a new axes on top...
'Color','none',... %# ... with no background color
'YAxisLocation','right',... %# ... located on the right
'XTick',[],'YTick',[],... %# ... with no x tick marks
'Box','off');
ylabel(hNewAxes,directions{2,1},'Rotation',-360); % yaxis label right
% Adjust position
ylabelh = get(gca,'YLabel');
Lpos = get(ylabelh,'Position');
set(gca,'YTick',[]);
set(ylabelh,'Position',Lpos+ [+Lpos(1)*0.02 0.05 0])
% -- And repeat for x axis -- %
% x axis labels
xlabel(directions{3,1},'Rotation',-360); % xaxis label bottom
% Adjust position
xlabelh = get(gca,'XLabel');
xlabpos = get(xlabelh,'Position');
set(gca,'XTick',[]);
rpos = get(xlabelh,'Position');
% do stuff ... as above
axesPosition = get(gca,'Position');
hNewAxes = axes('Position',axesPosition,... %# Place a new axes on top...
'Color','none',... %# ... with no background color
'XAxisLocation','top',... %# ... located on the right
'XTick',[],'YTick',[], ... %# ... with no x tick marks
'Box','off');
% xaxis label top
xlabel(hNewAxes,directions{4,1},'Rotation',-360);
Weirdly, if I run the code as a script it's fine (well, the labels get written over but I don't see the issue with the y axis labels) but if I run as a function multiple times in debug mode (which is currently the primary way I'm using it) then I see the above artefacts.
If you run your code then check at the end you will see that you have created 3 axes in your script. Run it once and they type get(gcf,'Children'). If you run it again you are adding another 2 axes, this continues everytime you run it.
Why does this happen?
Well your code creates 2 new axes handles (which you call the same name -> which you shouldn't do) - but only after you have already used gca -> i.e. the current axes (if none exists one is created).
I would advise you to completly reconfigure you code. Start by creating 2 axes before you do any plotting/labeling etc.. store the 2 axes variables independently which will allow you to refer to them as and when you need to.
Also it good practice to never rely on gca or gcf to get the currently axes or figure -> as it will eventually come back to bite you. Store the axes/figure handles and refer to them explicitly.

How can I edit the coordinate of a point marked on an image in MATLAB?

I am creating a GUI in Matlab and I need to move some data points using the mouse, for example:
imshow(someImage, [ ]), hold on;
plot(x, y, '*r')
I want to select a point from x and y vectors by clicking on it and move it using the mouse. How can I implement it?
You can use impoint from the Image Processing Toolbox since it already offers you the possibility to drag the point around. Therefore, create a figure and plot whatever you want. Then, call impoint(gca) so you can place a point on the current axes. After the point is drawn you can drag it around with your mouse. You can call impoint again for the second point and so on...
To get the position of the points, you want to store them in an impoint-array upon creation and then call getPosition with each of the points to get the coordinates.
Since you did not provide any code to extend with the desired functionality, I created a simple plot with two buttons as an example. Press "Add point" which allows you to place a new point. After the first click, you can move this point around. When all the points are added, click "Done" to read the final coordinates of the points.
figure; % create new figure
plot([0,1],[0,0],'r'); % plot something nice (your image)
ylim([-1,1]); % set limit of y-axis
h = impoint.empty; % define empty object array of type impoint
btnAdd = uicontrol('String','Add point',...
'Position',[90 60 70 30],...
'Callback', 'h(end+1)=impoint(gca);h(end).Deletable=0;wait(h(end))');
btnDone = uicontrol('String','Done',...
'Position',[165 60 40 30],...
'Callback', 'uiresume(gcbf)');
uiwait(gcf); % wait until 'Done' is pressed
delete(btnAdd); % revove the button
delete(btnDone); % revove the button
% get coordinates of points
pos = zeros(numel(h),2); % preallocate
for k = 1:numel(h)
pos(k,:) = getPosition(h(k)); % read coordinates of points
end
% evaluate points
positivePoints = sum(pos(:,2)>0) % count all points above 0

Ezpolar plots function string over polar axes

I'm using Ezpolar function in order to plot some graphics. Three of them have as maximum value 4, but the last one only has a bit more than 2.
When I plot them, the last one is plotted with it's function over it (seems like if ezpolar paints it some pixels after it maxium value used as radius).
Code and Generated Plot
% This subplot is used since I've 4 graphics to draw.
subplot(2,2,4)
ezpolar('0.25*(5 - 4*cosd(-180 * sin(t) ))');
title('D')
If I don't use this subplot, using a complete figure to draw the graphics seems fine. However, since I need to have all four of them together, it results in (will draw only the problematic one, subplot 2,2,4):
As you can see, r = 0.25 (5 - 4...) is plotted just over the polar axes.
Why is it happening? How can I fix it?
The issue appears to be with the position of that annotation and the fact that when you use a subplot, the limits on the radius of the polar plot actually change but the position of the annotation does not.
To combat this, you could actually compute the limits of the axes and change the position of the text to be explicitly outside of the plot.
hax = subplot(2,2,4);
p = ezpolar('0.25*(5 - 4*cosd(-180 * sin(t) ))');
% Get the handle to the label text object
label = findobj(hax, 'type', 'text');
% Figure out the current axes limits
ylims = get(hax, 'ylim');
% Add some padding (as a percent) to the position
padding = 0.2;
set(label, 'Position', [0 ylims(1)*(1 + padding), 0]);
A better way to do this would be to change the Units of the label to use Normalized units (relative to the axes) rather than Data units. This way, it will not change if the axes limits change.
hax = subplot(2,2,4);
p = ezpolar('0.25*(5 - 4*cosd(-180 * sin(t) ))');
% Get the handle to the label text object
label = findobj(hax, 'type', 'text');
set(label, 'Units', 'Normalized', 'Position', [0.5, -0.2]);

Enlarge figure in MATLAB

I want to create figure that is enlarged, I use:
fig = figure(1); %These two lines maximize the figure dialogue
set (fig, 'Units', 'normalized', 'Position', [0,0,1,1]);
The dialogue is enlarged. What should I do if I also want the graph inside this dialogue also enlarged? Although I can use "zoom in" and "pan" in the dialogue to enlarge and reposition my graph I want this be done automatically by codes.
Thanks a lot.
Update of my question:
I am trying to plot 3D block which the value is represented by color of each small unit block:
clear; close all; clc;
fig = figure(1);
set (fig, 'Units', 'normalized', 'Position', [0,0,1,1]);
fig_color='w'; fig_colordef='white';
cMap=jet(256); %set the colomap using the "jet" scale
faceAlpha1=1;
faceAlpha2=0.65;
edgeColor1='none';
edgeColor2='none';
NumBoxX=100;%box number in x direction
NumBoxY=100;%box number in y direction
NumBoxZ=5;%box number in z direction
fid = fopen('Stress.dat','r');
datacell = textscan(fid, '%f%f%f%f%f%f%f%f%f%f%f%f%f%f');
fclose(fid);
all_data = cell2mat(datacell);
M=zeros(NumBoxX,NumBoxY,NumBoxZ);
for i=1:NumBoxX
for j=1:NumBoxY
for k=1:NumBoxZ
num=k+NumBoxZ*(j-1)+NumBoxZ*NumBoxY*(i-1);
M(i,j,k)=all_data(num,4); %the forth column of all_data is dislocation density
end
end
end
indPatch=1:numel(M);
[F,V,C]=ind2patch(indPatch,M,'v'); %Call the function ind2patch in order to plot 3D cube with color
title('\sigma_{xy}','fontsize',20);
xlabel('y','fontsize',20);ylabel('x','fontsize',20); zlabel('z','fontsize',20); hold on;
set(get(gca,'xlabel'),'Position',[5 -50 30]);
set(get(gca,'ylabel'),'Position',[5 50 -15]);
set(get(gca,'zlabel'),'Position',[64 190 -60]);
patch('Faces',F,'Vertices',V,'FaceColor','flat','CData',C,'EdgeColor','k','FaceAlpha',0.5);
axis equal; view(3); axis tight; axis vis3d; grid off;
colormap(cMap); caxis([min(M(:)) max(M(:))]);
cb = colorbar;
set(get(cb,'title'),'string','Stress (MPa)','fontsize',20);
lbpos = get(cb,'title'); % get the handle of the colorbar title
set(lbpos,'units','normalized','position',[0,1.04]);
zoom(1.9);
I maximize the dialogue, read data from a file and use a function "ind2patch" found in internet to create boxes each has a color determined by a value assigned to it. At the last part I used zoom(1.9) to enlarge it but I want to shift the whole figure without moving the colorbar.
The following is the original picture before zoomed:
https://www.dropbox.com/s/xashny3w1fwcb2f/small.jpg?dl=0
The following picture is enlarged using zoom(1.9):
https://www.dropbox.com/s/0sfqq1lgo7cm5jd/large.jpg?dl=0
MyAxes=gca;
set(MyAxes,'Units','Normalized','position',[0.1,0.1,0.8,0.8]);
Note that the position you define is with respect to your axes parent, i.e. the figure.
If the figure you want to enlarge is not the current figure, you'll have to dig in your fig object's children in order to find your axes :
MyAxes=get(fig,'Children');
set(MyAxes,'Units','Normalized','position',[0.1,0.1,0.8,0.8]);
Note that, if your figure contains several subplots (thus several axes), you'll have to loop over all of them in order to enlarge them the way you want.
UPDATE : In order to reposition your graph as would the "pan" button do, you'll have to change your axes 'xlim' and 'ylim' properties. For example, if you want to move it 5% to the right and 10% to the top :
%Get current limits
MyXLimits=get(MyAxes,'xlim'); %1x2 vector [xmin,xmax]
MyYLimits=get(MyAxes,'ylim'); %1x2 vector [ymin,ymax]
%Calculate desired limits
MyNewXLimits=[MyXLimits(1)+0.05*(MyXLimits(2)-MyXLimits(1))...
MyXLimits(2)+0.05*(MyXLimits(2)-MyXLimits(1))];
MyNewYLimits=[MyYLimits(1)+0.1*(MyYLimits(2)-MyYLimits(1))...
MyYLimits(2)+0.1*(MyYLimits(2)-MyYLimits(1))];
% Set desired limits
set(MyAxes,'xlim',MyNewXLimits);
set(MyAxes,'ylim',MyNewYLimits);
Or if you know a priori the X and Y limits you want :
%Set desired limits directly
set(MyAxes,'xlim',[Myxmin Myxmax]);
set(MyAxes,'ylim',[Myymin Myymax]);
I think you can figure out how to zoom in/zoom out by yourself, as it also involves playing with the limits of your graph.

Creating Horizontal and Vertical lines with varying axes values? (MatLab)

I am trying to plot both horizontal and vertical lines on a histogram that will be accurate to changing limits on both the x and y axes. I was using the line(X,Y) function, but cannot find a useful way to get the lines to be set depending on the parameters of the graph window.
I'm not entirely clear on what you want but here's the simplest answer to what I think you want:
Makes a sample histogram
y = randn(100,1);
hist(y,10)
Get the current limits of the x and y axes
xlimits = get(gca, 'XLim');
ylimits = get(gca, 'YLim');
Computes a single numeric value to plot a horizontal line.You'll want to replace this with your specific function of the axes limits
halfpt = ((ylimits(2)-ylimits(1))/2) + ylimits(1);
line(xlimits, [halfpt halfpt])
I'm not sure, but from your comment I'm suspecting that you aren't changing your axes programmatically, with say set(gca,'Xlim', [0 10]) but that you want to be able to drag the axes of your your figure with the mouse, say by using that hand/pointer button in the figure editor. In which case, one solution is to make your figure a GUI and write a callback function that handles line plotting that is a function of the xlim and ylim. Here's an example that always keeps the line in the middle of the axes regardless of how they are dragged:
function myGUI
figure('WindowButtonMotionFcn',#myCallback)
y = randn(100,1);
hist(y,10)
function myCallback(src,eventdata)
xlimits = get(gca, 'XLim');
ylimits = get(gca, 'YLim');
halfpt = ((ylimits(2)-ylimits(1))/2) + ylimits(1);
lh = findall(gcf,'Type','Line');
delete(lh);
myline = line(xlimits, [halfpt halfpt])
end
end