Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problems in Changing Data Cursor Text

255 views
Skip to first unread message

Noushin Farnoud

unread,
Nov 1, 2007, 2:52:20 PM11/1/07
to
Hi All,
I have seen the Matlab Help example for "Change Data Cursor
Text". But it doesn’t solve my problem:

Imagine that you like to display information related to your
position but it's NOT the exact position values:

For example:

myData

Pos Name Status
--- ---- ---------
100 John Employee
120 Meg Manager
130 ....

And then you plot only the POS values. In data cursor mode,
I like to display NOT the pos values, but the Name and
Status relative to the Pos. Unfortunately; I can't find a
way to pass myData in myupdatefnc?

You can enable datacursor mode by:

fig=figure;
plot(Data(:,1));
dcm_obj = datacursormode(fig);
set(dcm_obj,'UpdateFcn',@myupdatefcn)

%---------------------------
In "myupdatefnc", The format @myupdatefnc seems to be fixed
(empt,event_obj) and changing it to (@myupdatefnc,arg1)
seems to be impossible:

This is what I like to do:
function txt = myupdatefcn(empt,event_obj)
pos = get(event_obj,'Position');
txt={['Name:',myData(Pos,2)];['Status:',myData(Pos,3)]};

Somehow I must be able to access myData?!

I appreciate your help on this.

Many Thanks
Noushin

Thomas

unread,
Nov 1, 2007, 7:20:18 PM11/1/07
to
Hi Noushin,

You can put your data in the 'appdata' of the object being
plotted. The handle to that object is given to you in the
target field of the event_obj when the updatefcn is called.
Here's some demonstration code, that tests the location of
the mouse click, and returns one of two messages from the
appdata depending on its value.

function demo
figh = figure;
imh = imagesc(rand(10));
setappdata(imh, 'messages', {'secret msg high' 'secret msg
low'})

dcmh = datacursormode(figh);
set(dcmh, 'UpdateFcn', @myupdatefcn,...
'enable', 'on');

function [txt] = myupdatefcn(obj, event_obj)


pos = get(event_obj, 'Position');

msgs = getappdata(event_obj.target, 'messages');
if pos(1)>5,
txt = msgs{1};
else
txt = msgs{2};
end


Note that you could also put the data in the appdata of the
plot axes, or figure, and access it by looking for ancestor
of the 'target' handle. That way it could be shared by
several different plots.

Note also that this data won't be updated automatically
after you save it to the appdata. That is, you will be
accessing the copy you stored in the appdata, and not the
variable that is still the original workspace...

-Tom


"Noushin Farnoud" <nfar...@bcgsc.ca> wrote in message
<fgd794$s35$1...@fred.mathworks.com>...

Thomas

unread,
Nov 1, 2007, 7:22:37 PM11/1/07
to
Hi Noushin,

You can put your data in the 'appdata' of the object being
plotted. The handle to that object is given to you in the
target field of the event_obj when the updatefcn is called.
Here's some demonstration code, that tests the location of
the mouse click, and returns one of two messages from the
appdata depending on its value.

function demo
figh = figure;
imh = imagesc(rand(10));
setappdata(imh, 'messages', {'secret msg high' 'secret msg
low'})

dcmh = datacursormode(figh);
set(dcmh, 'UpdateFcn', @myupdatefcn,...
'enable', 'on');

function [txt] = myupdatefcn(obj, event_obj)

pos = get(event_obj, 'Position');

msgs = getappdata(event_obj.target, 'messages');
if pos(1)>5,
txt = msgs{1};
else
txt = msgs{2};
end


Note that you could also put the data in the appdata of the
plot axes, or figure, and access it by looking for ancestor
of the 'target' handle. That way it could be shared by
several different plots.

Note also that this data won't be updated automatically
after you save it to the appdata. That is, you will be
accessing the copy you stored in the appdata, and not the
variable that is still the original workspace...

-Tom


"Noushin Farnoud" <nfar...@bcgsc.ca> wrote in message
<fgd794$s35$1...@fred.mathworks.com>...

Steven Lord

unread,
Nov 1, 2007, 11:23:20 PM11/1/07
to

"Noushin Farnoud" <nfar...@bcgsc.ca> wrote in message
news:fgd794$s35$1...@fred.mathworks.com...

Use an anonymous function.


function updatedatacursor
h = plot(1:10);
dcm = datacursormode(gcf);
data = 'The data cursor is at (%f, %f)';
set(dcm, 'Update', @(obj, event_obj) myupdatefcn(data, event_obj));
datacursormode on

function output = myupdatefcn(string, event_obj)
output = sprintf(string, event_obj.Position);


--
Steve Lord
sl...@mathworks.com


Richard Sims

unread,
Jan 17, 2013, 1:37:16 PM1/17/13
to
I realize this question was posted a long time ago but i have the same problem and am struggling to interpret the answer you posted. I have latitude and longitude plotted and i have a 3rd varibale cruiseid located in the workspace, which is not plotted but is in the same dimensions as lat and long, i want to find out what the cruise id is when i click on the point. I have a similar code to Noushin.

figure(1)
scatter(longitude,latitude)
dcm_obj = datacursormode(figure (1) );
set(dcm_obj,'UpdateFcn',@myupdatefcn, 'enable', 'on')


function txt = myupdatefcn(empt,event_obj)
% Customizes text of data tips
pos = get(event_obj,'Position');
txt = {['long: ',num2str(pos(1))],['lat: ',num2str(pos(2))],};











"Steven Lord" <sl...@mathworks.com> wrote in message <fge578$b3j$1...@fred.mathworks.com>...
>
> "Noushin Farnoud" <nfar...@bcgsc.ca> wrote in message
> news:fgd794$s35$1...@fred.mathworks.com...
> > Hi All,
> > I have seen the Matlab Help example for "Change Data Cursor
> > Text". But it doesn’t solve my problem:

Richard Sims

unread,
Jan 17, 2013, 2:28:07 PM1/17/13
to
For anyone who comes across this i found the solution on stack overflow

--------------------------------------------------------------------------------------------------------
The questioner wants to see exactly the data in the not-shown array, rather than just a color. This is a job for custom data cursor function. Below I've implemented this using purely anonymous functions; doing it within a function file would be slightly more straightforward.

#% Step 0: create a function to index into an array...
#% returned by 'get' all in one step
#% The find(ismember... bit is so it returns an empty matrix...
#% if the index is out of bounds (if/else statements don't work...
#% in anonymous functions)
getel = @(x,i) x(find(ismember(1:numel(x),i)));

#% Step 1: create a custom data cursor function that takes...
#% the additional matrix as a parameter
myfunc = @(obj,event_obj,data) {...
['X: ' num2str(getel(get(event_obj,'position'),1))],...
['Y: ' num2str(getel(get(event_obj,'position'),2))],...
['V: ' num2str(getel(data,get(event_obj,'dataindex')))] };

#% Step 2: get a handle to the datacursormode object for the figure
dcm_obj = datacursormode(gcf);

#% Step 3: enable the object
set(dcm_obj,'enable','on')

#% Step 4: set the custom function as the updatefcn, and give it the extra...
#% data to be displayed
set(dcm_obj,'UpdateFcn',{myfunc,V})

Now the tooltip should display the extra data. Note that if you change the data in the plot, you'll need to repeat Step 4 to pass the new data into the function.


thanks to tmpearce who originally contributed this code. All you need to do is simply change the value of V in step 4 to your data of interest :)

Richard Sims

unread,
Jan 17, 2013, 2:46:08 PM1/17/13
to
It is also possible to add multiple independent variable labels in datacursormode
At the start of step 2 in the code above you can add , extra data points eg
myfunc = @(obj,event_obj,data,data2)

Where in the previous code the line appears
['V: ' num2str(getel(data,get(event_obj,'dataindex')))] };
add a new line underneath
['W: ' num2str(getel(data2,get(event_obj,'dataindex')))]};

and on the bottom line where
set(dcm_obj,'UpdateFcn',{myfunc,V})
replace with
set(dcm_obj,'UpdateFcn',{myfunc,V,W})


This worked for me and now when i go into data cursor mode i can see my x,y coordinates and two other pieces of information like temperature and salinity. I hope this helps someone
Rich
0 new messages