Moving the data tip horizontally with arrow keys

35 views (last 30 days)
Dear all,
I have the following problem. I am plotting a 2D-Matrix with
figure
pcolor(int_counts)
Now, I wrote a small script the redraws a plot every time the data tip in this pcolor plot is changed. Fair enough, it works. The problem now is, that I would like to use the arrow keys on my keyboard to move around the data tip. This works very nice with the up- and down-arrow (vertical direction in the plot), but it doesn't work out for the left- and right-arrow. When I try to use them both the vertical and the horizontal position in the plot change.
Does somebody know how I can solve the problem?
Best regards!
  2 Comments
Geoff Hayes
Geoff Hayes on 29 Jan 2016
Sven - can you post your code showing how the up and down arrows are used to move the data tip? And how you incorporated the left and right arrows? A small example may be necessary to show us what you have tried.
Sven Borghardt
Sven Borghardt on 29 Jan 2016
Hi Geoff,
I did not write any code for that. It's implemented in Matlab. You can try it:
pcolor(hadamard(20))
colormap(gray(2))
axis ij
axis square
Writing this, I notice that this is a bad example because it works. But it does not work for non-square matrices. Try
mat = hadamard(20)
pcolor(mat(1:20,1:10))
colormap(gray(2))
axis ij
axis square
Then, put a data tip somewhere and move with your arrow keys to the right, you'll see you don't end up where you are supposed to end up. In this particular example pressing the right arrow once moves you 10 data points further, but this is not equivalent to one column.

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 30 Jan 2016
Sven - you are right, the behaviour of the left and right arrow keys is unexpected (at least to me anyway). The documentation from http://www.mathworks.com/help/matlab/creating_plots/data-cursor-displaying-data-values-interactively.html states you can move the marker using the arrow keys and the mouse. The up and right arrows move the marker to data points having greater index values in the data arrays. The down and left arrow keys move the marker to data points having lesser index values. So "moving the right arrow moves the marker to a data point with a greater index" means what exactly?
Anyway, for the example that you presented above, there is a way to control the position of the data tip though it may not be practical for what you have intended. From http://undocumentedmatlab.com/blog/controlling-plot-data-tips you can do something like the following
function moveDataTip
close all;
% create figure with dummy data
hFig = figure;
mat = hadamard(20);
hTarget = pcolor(mat(1:20,1:10));
colormap(gray(2));
axis ij;
axis square;
% create datacursor object
dcm_obj = datacursormode(hFig);
%create a data tip within the target
hDataTip = dcm_obj.createDatatip(hTarget);
% get current position of datacursor object
info_struct = getCursorInfo(dcm_obj);
% get the data where the datacursor can move to
xData = get(info_struct.Target,'XData');
yData = get(info_struct.Target,'YData');
% set the figure to respond to key events
set(hFig,'KeyPressFcn',@keyDownListener);
function keyDownListener(hObj,eventdata)
% intercept the key press and move the datatip
shouldMoveDataTip = false;
curPos = get(hDataTip,'Position');
key = eventdata.Key;
if strcmpi(key,'leftarrow')
if curPos(1) > min(yData)
curPos(1) = curPos(1) - 1;
shouldMoveDataTip = true;
end
elseif strcmpi(key,'rightarrow')
if curPos(1) < max(xData)
curPos(1) = curPos(1) + 1;
shouldMoveDataTip = true;
end
elseif strcmpi(key,'uparrow')
if curPos(2) > min(yData)
curPos(2) = curPos(2) - 1;
shouldMoveDataTip = true;
end
elseif strcmpi(key,'downarrow')
if curPos(2) < max(yData)
curPos(2) = curPos(2) + 1;
shouldMoveDataTip = true;
end
end
if shouldMoveDataTip
set(hDataTip,'Position',curPos,'String',sprintf('X: %d\nY: %d\n,Z: %d',curPos(1),curPos(2),curPos(3)));
end
end
end
The above assumes that the target upon which you are shifting the data tip is a grid with integer positions (which may not always be the case). So this might not work, or it might provide a hint of what can be done to better solve your problem.
  2 Comments
Sven Borghardt
Sven Borghardt on 31 Jan 2016
Dear Geoff,
thank you so much for your answer. It is the solution I was looking for!
Sven

Sign in to comment.

More Answers (1)

John BG
John BG on 30 Jan 2016
remove
axis ij
this command is useful when working on images that have [0,0] on top left corner of the image, which is not the case here, is it?
does it help? please click on thumbs up thanks in advance
John
  1 Comment
Sven Borghardt
Sven Borghardt on 30 Jan 2016
Sorry, it does not help. It just changes the orientation of the image.

Sign in to comment.

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!