Code covered by the BSD License  

Highlights from
Custom Data Cursor

4.75

4.8 | 4 ratings Rate this file 14 Downloads (last 30 days) File Size: 2.18 KB File ID: #18773
image thumbnail

Custom Data Cursor

by Dave Van Tol

 

15 Feb 2008 (Updated 22 May 2008)

This function allows a user to label each point in a data series with custom labels.

| Watch this File

File Information
Description

customDataCursor allows a user to label each point in a data series with custom labels. When the data cursor mode is enabled (DATACURSORMODE ON), clicking on a data point will display the custom label, then the x and y values of the data point. If more than one data point of the data series is in the same location, the data cursor will display all of the labels first, then the x and y locations.

Usage: customDataCursor(H,DATALABELS) applies the custom labels found in cell array DATALABELS to the line with handle H. DATALABELS must be a cell array with the same length as 'XDATA' and 'YDATA' in the line; in other words, there must be a label for each data point in the line.

dcmH = customDataCursor(H,DATALABELS) returns the handle to the data cursor in dcmH.

Note: CUSTOMDATACURSOR uses the 'UserData' property of a line to store and retrieve the labels. If that property is used by another portion of a user's program, this function may be broken, or this function may break the user's program.

Example:
  % generate some data and chart it
  N = 20;
  x = rand(N,1);
  y = rand(N,1);
  h = plot(x,y,'ko');
  % make up some labels and apply them to the chart
  labels = cell(N,1);
  for ii = 1:N
      labels{ii} = ii;
  end
  customDataCursor(h,labels)

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (7)
05 Apr 2008 Ahmed Andalusi

Thank you for submitting this! It saved me some time!

I found a bug. The program crashes when one of the X and Y values is negative (That's cause rows in string cell arrays need to be equal length). I fixed this bug by putting everything in one row.

21 May 2008 Dave Van Tol

Ahmed:

Thanks for the bug report and the review.

I wasn't able to recreate the error, although I think I understand it from your explanation. I have changed the offending array to a cell array rather than a string array, so lines of different length are acceptable. Let me know if it doesn't work.

23 Sep 2010 Richard Crozier

Very useful in showing my how to write my own custom datatip function!

04 Mar 2011 Emily

I actually just figured out a solution:

if length(datapoint)>1
    txt = labels{datapoint(1)};
    for i = 2:length(datapoint)
       txt = [txt ';' labels{datapoint(i)}];
    end
else
  txt = labels{datapoint};
end

output_txt = { txt;...
    ['X: ',num2str(pos(1),4)];...
    ['Y: ',num2str(pos(2),4)] };

Probably not the best way to code it, but it works...

26 Jul 2011 Adam Auton  
26 Jul 2011 Adam Auton

Excellent - makes the whole process much easier

21 Oct 2011 Ben v

I run this thing all the time, very useful. Is there any way to save the labels in the figure so I can just save a .fig and send it to a collaborator?

Please login to add a comment or rating.
Updates
18 Feb 2008

-Added the ability to return a handle to the cursor
-Improved the help

22 May 2008

Modified code to fix bug reported by Ahmed Andalusi. Although I could not recreate the error message reported, I think I understand the offending line of code and have changed it to use a cell array rather than a string array. Thanks, Ahmed.

Tag Activity for this File
Tag Applied By Date/Time
annotation Dave Van Tol 22 Oct 2008 09:47:51
customization Dave Van Tol 22 Oct 2008 09:47:51
data cursor datacursor plot annotate annotation Dave Van Tol 22 Oct 2008 09:47:51

Contact us at files@mathworks.com