MATLAB: Moving the data tip horizontally with arrow keys

data tippcolor

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!

Best Answer

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.
Related Question