MATLAB: How do i calculate True data values being plotted from a mouse coordinate

MATLAB and Simulink Student Suitematlab coordinatesmouse eventsxdata and ydata from mouse coordinate

I have been trying for hours to do this and i cant figure it out. Here is my problem i am plotting 4 subplots in one figure. The plots come out like i need them and here is an example of one of them
The X is DateTime series that is read in from a csv file, the Y is various statistics that pertain to that DateTime. What i am attempting at doing is to hover my cursor at some point on the subplot and retrieve the values that are not the global coordinates but the real values as they are considered in the plot.
As an example of wanted behavior when i hover over near 1,0 on the subplot above i should get the datetime of Jan 07,20,00(or something close) and the Y value of -1080(or something close)
Here is what i have done so far my main figure is called 'fig' then after everything is plotted i set the callback function
set(fig,'WindowButtonMotionFcn',@mouseMove);
function mouseMove(object,eventdata)
c = get(0,'PointerLocation');
datax1 = get(pl1,'XData');
datay1 = get(pl1,'YData');
pos = getpixelposition(p1);
dim1 = [1420 917 300 20];
legendString = ['(X,Y) = (', num2str(c(1,1)),', ',num2str(c(1,2)), ')'];
h1 = uicontrol('style','text','posit',dim1,'backg',mainBackColor,...
'fontsize',12,'fontweight','bold','string',legendString,...
'ForegroundColor', nearWhite);
drawnow;
end
At the moment it just creates a text field with the global x and y values of the mouse. pl1 is the plot call for one of the subplots. datax1 and datay1 do have the values for that specific plot i just dont know how to turn c(1) and c(2) into a value from datax1 and datay1 that is visually closest to those mouse coordinates.
If anything is missing for you MatLab wizards please ask i can provide more code. Any help or guidance to how to do this is appreciated.

Best Answer

ayar - since you have four subplots (or axes) you will need to determine which axes your mouse cursor is hovering over before you try to determine the (x,y) coordinate for that axes. For example, suppose we plot four sine and cosine curves as
axesHandles = [];
hFig = figure;
xx = -2*pi:0.001:2*pi;
axesHandles = subplot(2,2,1);
plot(xx,sin(xx));
axesHandles = [axesHandles subplot(2,2,2)];
plot(xx,cos(xx));
axesHandles = [axesHandles subplot(2,2,3)];
plot(xx,5*sin(xx));
axesHandles = [axesHandles subplot(2,2,4)];
plot(xx,5*cos(xx));
With the above, we create the four subplots and store the handle to each axes. We then assign the WindowButtonMotionFcn as
set(hFig,'WindowButtonMotionFcn',@mouseMove);
hText = [];
where hText will be the handle to our text graphics object. (We hold on to this handle because we don't want to create a new text graphics object whenever we move the mouse.) Now are callback will look like
function mouseMove(hObject,eventdata)
% get the position of the mouse
figCurrentPoint = get(hObject, 'CurrentPoint');
position = get(hObject, 'Position');
xCursor = figCurrentPoint(1,1)/position(1,3); % normalize

yCursor = figCurrentPoint(1,2)/position(1,4); % normalize
% iterate over each axes (subplot) to see where the mouse cursor is
% hovering over
isOverAxes = 0;
for k=1:length(axesHandles)
hAxes = axesHandles(k);
% get the position of the axes within the GUI
axesPos = get(hAxes,'Position');
minx = axesPos(1);
miny = axesPos(2);
maxx = minx + axesPos(3);
maxy = miny + axesPos(4);
% is the mouse down event within the axes?

if xCursor >= minx && xCursor <= maxx && yCursor >= miny && yCursor <= maxy
currentPoint = get(hAxes,'CurrentPoint');
x = currentPoint(2,1);
y = currentPoint(2,2);
textString = sprintf('(X,Y) = (%.6f,%.6f)', x, y);
if isempty(hText)
hText = uicontrol('Style', 'text','BackgroundColor', 'r',...
'FontSize',10,'FontWeight', 'bold','ForegroundColor', 'k');
end
set(hText,'Visible','on','Position', [figCurrentPoint(1,1) figCurrentPoint(1,2) 150 20], 'String', textString);
isOverAxes = true;
break;
end
if ~isOverAxes && ~isempty(hText)
set(hText,'Visible','off');
end
end
end
In the above, we get the current position of the mouse cursor and normalize it so that we can compare it to the normalized positions of the four axes. We loop over each axes until we find the one which satisfies
% is the mouse down event within the axes?
if xCursor >= minx && xCursor <= maxx && yCursor >= miny && yCursor <= may
We then get the current point within the axes which will be your "true" plotted data values. The text floats to the current position of the mouse cursor (rather than being static). See the attached for the full example.