File Exchange

image thumbnail


version (276 KB) by Chad Greene
Easily get z values of pcolor or image plots from mouse clicks.


Updated 08 Dec 2014

View License

Sometimes I plot a pcolor or image plot and I want to know with some precision what values exist a specific locations. The Data Cursor tool in the figure window does not return z values for pcolor plots, so I end up with my eyes darting back and forth between the location of interest in the pcolor plot and the colorbar, trying to pin down the z value at the interesting location.
This function temporarily prints z values corresponding to clicked points on a surface or image. If multiple surfaces or images exist in the same axes, clickz first looks for surfaces and determines the one on the top of the graphical stack is the relevant one. If no surfaces exist, but an image or multiple images exist, clickz probes the image on the top of the graphical stack.

To find "z" values of an existing image or pcolor plot, simply type clickz and start clicking on areas of interest. Instead of clicking, you may also hit any key on the keyboard except the keys listed below which perform the following functions:

Carriage return terminates data entry.
+ or z zooms in, centered on current cursor location.
- or x zooms out, centered on current cursor location.

Occasionally, linear interpolation between plotted data points fails, such as when xdata and ydata are not perfectly monotonic and plaid. I've only run into this problem when plotting polar stereographic data referenced one latitude, onto a polar stereographic map referenced to another latitude. If for some reason linear interpolation fails, clickz will attempt to find the nearest x,y data points by Euclidean distance, and will print z data at that nearby location. In such a case, any output x,y,z data will reflect the nearby point instead of the clicked point.

z = clickz(...)
[x,y,z] = clickz(...)
[x,y,z,h] = clickz(...)

clickz temporarily prints a z value on a plot at each click location. Previous printed values are deleted with each new click. clickz continues to run until user hits Return on the keyboard.

clickz(N) performs clickz N times or until the user hits Return on the keyboard. If N is specified with 'keep' and/or text formatting, N must be the first input argument.

clickz(...,ax) specifies an axis handle ax on which to use clickz.

clickz(...,'keep') does not delete printed points.

clickz(,...'TextProperty',TextValue) formats printed text. Multiple text properties and values may be specified, including fontsize, color, background, etc.

z = clickz(...) returns an array of clicked z values.

[x,y,z] = clickz(...) returns clicked x, y, and z values.

[x,y,z,h] = clickz(...) also returns text object handle h when the 'keep' command is used and all four outputs are requested by the user.

Cite As

Chad Greene (2020). clickz (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (1)

David J. Mack

Hey Chad!
Very elaborate solution for your problem!

However, if you want to use the built-in functionality, use the following approach:
1. Use the 'Data Cursor' tool on any point in the PCOLOR surface.
2. Right-click the datatip and select "Edit Text Update Function..."
3. Copy the following code below at the end of the callback function.
4. Save the callback (e.g. Clb_datatipPCOLOR.m).
5. Enjoy the updated datatip.

Callback code:

Target = get(event_obj, 'Target');

%Get X id.
X = get(Target,'XData');
[XDiff,XIdPre] = min(abs(X(:)-pos(1))); %FINDCLOSESTID2VAL with Linear Id
IsMatrixX = ~(isscalar(X) || isrow(X));
if IsMatrixX
[YIdX,XId] = ind2sub(size(X),XIdPre);
XId = XIdPre;

%Get Y id.
Y = get(Target,'YData');
[YDiff,YIdPre] = min(abs(Y(:)-pos(2))); %FINDCLOSESTID2VAL with Linear Id
IsMatrixY = ~(isscalar(Y) || iscolumn(Y));
if IsMatrixY
[YId,XIdY] = ind2sub(size(Y),YIdPre); %Keep braces, otherwise IND2SUB simply returns the input id.
YId = YIdPre;

%Check if columns/rows are all the same for one of them.
if IsMatrixY && all(Y(2:end,XId)==Y(1,XId))
YId = YIdX;
elseif IsMatrixX && all(X(YId,2:end)==X(YId,1))
XId = XIdY;

C = get(Target,'CData');
output_txt{end+1} = ['C: ',num2str(C(YId,XId),4)];

The only drawback at that solution is that you always have to reestablish your new callback via "Select Text Updata Function..." in each new plot.

Greetings, David


Forgot to update the zip file last time.

Now optionally returns text handle. Will attempt a nearest-neighbor search if linear interpolation fails. Can specify an axis other than gca.

MATLAB Release Compatibility
Created with R2014b
Compatible with any release
Platform Compatibility
Windows macOS Linux