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