How to create a crosshair that shows the position of impoint in a 2 dimensional plot?

6 views (last 30 days)
Hello everyone,
I am new to Matlab and am trying to achieve this: I want to create a figure with two subplots. In the upper subplot I want to show the acquired data, and in the lower subplot I want to show a profile along the y-axis of the data plot. The aim is to use the draggable point with impoint so the user can see the "evolution" of the profile by just dragging that point. So ginput wouldn't quite do the trick for me. Till here everything works fine.
Next I tried to implement the cross hair by just plotting two white lines in the data plot and ran into some problems: The whole idea of the crosshair is to better see the position of the impoint and the possibility to make screenshots of the data so people can actually see where the profile they are seeing below is taken from. The crosshair does work, the only problem is that it just keeps adding new white lines! So if one drags the impoint rather sooner than later the whole data plot is white! My question then is how to prevent this from happening? I only want the actual position to be drawn on the data plot? I would be very grateful for any suggestions, I have searched the internet but could not find a solution.
Here is my simple example code of a first attempt to solve the problem: It seems to go in the right direction but doesn't work yet. I always get and never understand:
??? Error using ==> getappdata
Invalid object handle
Error in ==> impoint>impointAPI/dragMotion at 499
if ~isempty(getappdata(h_group,'roiObjectReference'))
Error in ==> iptaddcallback>callbackProcessor at 150
fun(varargin{:});
??? Error while evaluating figure WindowButtonMotionFcn
If anybody can give me any suggestions I would be happy beyond measure, I have been working on this for quite a while and have no idea how to solve this. Thank you very much in advance for any answer, I appreciate it. And here is my actual (bit messy variable names, I know) code:
function [fig] = impointtest
%Creating the example data
matrix=magic(10);
a = 1:10;
b= 1:10;
fig = figure(1);
sub1=subplot(211);
cla(gca);
testhandle=@testfunc;
h(1) = pcolor(a,b,matrix);
set(h(1),'EdgeColor','none');
hold on;
%psition Callback
P0 = impoint(gca);
addNewPositionCallback(P0,@(P0) testhandle(P0(1),P0(2),matrix));
% Construct boundary constraint function
fcn = makeConstrainToRectFcn('impoint',get(gca,'XLim'),get(gca,'YLim'));
% Enforce boundary constraint function using setPositionConstraintFcn
setPositionConstraintFcn(P0,fcn);
end
function [h2,h3] = testfunc(x,y,matrix);
sub1=subplot(211);
%From what I understood, the children are the problem: The last two cross
%hair lines are simply added to this nx1 'Children' vector on top (the last to entries of
%this array are the oldest). So for every movement of the impoint two new
%entries are created (each for one cross hair line). So by my logic I have to delete all but the first two
%(the last cross hair)and last two entries(entries for the subplot) of the
%'Children' vector and copy this new shortened 'Children' vector back into the
%subplot.
%subchild work around to delete older crosshair lines:
sub1child = get(sub1, 'Children')
%Get lenght of array containing the children
sublength=length(sub1child)
%The first time the function is called, no crosshair exists, so only 2
%entries
if (sublength==2)
%disp('case 2')
% subl = length(sub1child);
lastsub1child = sub1child;
copyobj(lastsub1child,sub1);
delete(sub1child)
end
%
if (sublength>=4)
disp('case 4')
% subl = length(sub1child)
whos sub1child
sub1T=sub1child'
newsub1child = sub1T([1:2 (sublength-1):sublength])
copyobj(newsub1child',sub1);
delete(sub1child)
end
%end of subchild workaround
%Create crosshair lines
linex(1:length(matrix(:,1))) = x;
liney(1:length(matrix(1,:))) = y;
xvec = (1:length(matrix(1,:)));
yvec = (1:length(matrix(:,1)));
h2= plot(linex,yvec, 'LineWidth', 2,'Color','white');
h3= plot(xvec,liney, 'LineWidth', 2,'Color','white');
sub1child = get(sub1, 'Children');
sub2=subplot(212);
%display actual position of impoint in title
plot(matrix(:,floor(x)));
title(sprintf('Position is: %0.1f,%0.1f Value is: (%0.1f)',floor(x),floor(y),matrix(floor(x),floor(y))));
end

Answers (0)

Categories

Find more on Visual Exploration in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!