Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Event Object for Datacursormode

Subject: Event Object for Datacursormode

From: Nima Nikvand

Date: 30 Dec, 2012 05:21:09

Message: 1 of 8

Hello everyone,

I am trying to capture data information on the plot using datacursormode, my general format is as follows:

fig = figure();
dcmObj=datacursormode(fig);
t=0:0.01:10;
x = sin(t);
plot(t,x);
        set(dcmObj,'Enable','on',...
          'SnapToDataVertex','on', ...
           'DisplayStyle','datatip',...
           'UpdateFcn',@datatextbox);

where datatextbox is my update function and its general format is:

function [txt,pos] = datatextbox(~,event_obj)

and this works very well. However, I'd like to be able to get the output of my datatextbox function and I was wondering if I can some how call the function directly by passing the event_obj to it. I'd greatly appreciate your suggestions, or any other solutions to get the output arguments.

Subject: Event Object for Datacursormode

From: Matt J

Date: 30 Dec, 2012 06:33:20

Message: 2 of 8

"Nima Nikvand" <nnikvand@uwaterloo.ca> wrote in message <kboj05$ahe$1@newscl01ah.mathworks.com>...
>
> function [txt,pos] = datatextbox(~,event_obj)
>
> and this works very well. However, I'd like to be able to get the output of my datatextbox function and I was wondering if I can some how call the function directly by passing the event_obj to it. I'd greatly appreciate your suggestions, or any other solutions to get the output arguments.
=============

If you make datatextbox a nested function, every variable in its workspace will also be available in the workspace in which it is nested.

Subject: Event Object for Datacursormode

From: Nima Nikvand

Date: 30 Dec, 2012 06:45:25

Message: 3 of 8

Hi Matt,

Thank you for your reply. Can you please give me an example of how to make it into a nested function? much appreciated.

"Matt J" wrote in message <kbon7f$njj$1@newscl01ah.mathworks.com>...
> "Nima Nikvand" <nnikvand@uwaterloo.ca> wrote in message <kboj05$ahe$1@newscl01ah.mathworks.com>...
> >
> > function [txt,pos] = datatextbox(~,event_obj)
> >
> > and this works very well. However, I'd like to be able to get the output of my datatextbox function and I was wondering if I can some how call the function directly by passing the event_obj to it. I'd greatly appreciate your suggestions, or any other solutions to get the output arguments.
> =============
>
> If you make datatextbox a nested function, every variable in its workspace will also be available in the workspace in which it is nested.

Subject: Event Object for Datacursormode

From: Matt J

Date: 30 Dec, 2012 07:17:21

Message: 4 of 8

"Nima Nikvand" <nnikvand@uwaterloo.ca> wrote in message <kbonu5$ppk$1@newscl01ah.mathworks.com>...
> Hi Matt,
>
> Thank you for your reply. Can you please give me an example of how to make it into a nested function? much appreciated.

Run the following to get the general idea.

function test


   txtCopy=[];
   posCopy=[];
   
  
   datatextbox(2,1);
    
    txtCopy, %3
    posCopy, %1
    
   datatextbox(4,4);
    
    txtCopy, %8
    posCopy, %0



   function [txt,pos] = datatextbox(a,b)

       txt=a+b;
       pos=a-b;
       
       txtCopy=txt;
       posCopy=pos;
   
       
   end


end

Subject: Event Object for Datacursormode

From: Matt J

Date: 30 Dec, 2012 08:05:21

Message: 5 of 8

"Matt J" wrote in message <kbon7f$njj$1@newscl01ah.mathworks.com>...
> "Nima Nikvand" <nnikvand@uwaterloo.ca> wrote in message <kboj05$ahe$1@newscl01ah.mathworks.com>...
> >
> > function [txt,pos] = datatextbox(~,event_obj)
> >
> > and this works very well. However, I'd like to be able to get the output of my datatextbox function and I was wondering if I can some how call the function directly by passing the event_obj to it. I'd greatly appreciate your suggestions, or any other solutions to get the output arguments.
> =============
>
> If you make datatextbox a nested function, every variable in its workspace will also be available in the workspace in which it is nested.

You could also call setappdata from within datatextbox and store the txt,pos output data to the parent figure. Pass the figure handle to datatextbox using this syntax

 set(dcmObj,'UpdateFcn',{@datatextbox, ancestor(dcmObj,'figure')});

Then use getappdata to pull it out later.

Subject: Event Object for Datacursormode

From: Nima Nikvand

Date: 30 Dec, 2012 20:08:07

Message: 6 of 8

Hello Matt,

Thanks for your replies to both my posts. I modified my datatextboxfunction as follows to store my desired variables in figure handles:

 function [txt,pos] = datatextbox(~,event_obj,ancestor)
        pos = get(event_obj,'Position');
        
        txt = {['Vector Length: ',num2str(pos(1))],...
               ['MOS: ',num2str(pos(2))]};
           setappdata(handles.figure1,'img',pos);
           %setappdata(handles.figure2,'txtx',txt);

    end

However, now my data tip is not showing the correct information (Vector length and Mos) , and shows Error in Custom data tip function instead. When I remove the setappdata function it starts working again.

"Matt J" wrote in message <kbosk1$b1q$1@newscl01ah.mathworks.com>...
> "Matt J" wrote in message <kbon7f$njj$1@newscl01ah.mathworks.com>...
> > "Nima Nikvand" <nnikvand@uwaterloo.ca> wrote in message <kboj05$ahe$1@newscl01ah.mathworks.com>...
> > >
> > > function [txt,pos] = datatextbox(~,event_obj)
> > >
> > > and this works very well. However, I'd like to be able to get the output of my datatextbox function and I was wondering if I can some how call the function directly by passing the event_obj to it. I'd greatly appreciate your suggestions, or any other solutions to get the output arguments.
> > =============
> >
> > If you make datatextbox a nested function, every variable in its workspace will also be available in the workspace in which it is nested.
>
> You could also call setappdata from within datatextbox and store the txt,pos output data to the parent figure. Pass the figure handle to datatextbox using this syntax
>
> set(dcmObj,'UpdateFcn',{@datatextbox, ancestor(dcmObj,'figure')});
>
> Then use getappdata to pull it out later.

Subject: Event Object for Datacursormode

From: Nima Nikvand

Date: 30 Dec, 2012 20:51:07

Message: 7 of 8

Hi Matt,

I am getting an empty matrix as the output of my ancestor function, a simple code like this for example:

fig = figure(1);
x=0:0.01:10,
y = sin(x);
plot(x,y);

dcmObj=datacursormode(fig);
p=ancestor(dcmObj,'figure')

is also returning p=[].

"Matt J" wrote in message <kbosk1$b1q$1@newscl01ah.mathworks.com>...
> "Matt J" wrote in message <kbon7f$njj$1@newscl01ah.mathworks.com>...
> > "Nima Nikvand" <nnikvand@uwaterloo.ca> wrote in message <kboj05$ahe$1@newscl01ah.mathworks.com>...
> > >
> > > function [txt,pos] = datatextbox(~,event_obj)
> > >
> > > and this works very well. However, I'd like to be able to get the output of my datatextbox function and I was wondering if I can some how call the function directly by passing the event_obj to it. I'd greatly appreciate your suggestions, or any other solutions to get the output arguments.
> > =============
> >
> > If you make datatextbox a nested function, every variable in its workspace will also be available in the workspace in which it is nested.
>
> You could also call setappdata from within datatextbox and store the txt,pos output data to the parent figure. Pass the figure handle to datatextbox using this syntax
>
> set(dcmObj,'UpdateFcn',{@datatextbox, ancestor(dcmObj,'figure')});
>
> Then use getappdata to pull it out later.

Subject: Event Object for Datacursormode

From: Steven_Lord

Date: 31 Dec, 2012 04:05:43

Message: 8 of 8



"Nima Nikvand" <nnikvand@uwaterloo.ca> wrote in message
news:kbq6v7$olu$1@newscl01ah.mathworks.com...
> Hello Matt,
>
> Thanks for your replies to both my posts. I modified my
> datatextboxfunction as follows to store my desired variables in figure
> handles:
>
> function [txt,pos] = datatextbox(~,event_obj,ancestor)
> pos = get(event_obj,'Position');
> txt = {['Vector Length: ',num2str(pos(1))],...
> ['MOS: ',num2str(pos(2))]};
> setappdata(handles.figure1,'img',pos);
> %setappdata(handles.figure2,'txtx',txt);
>
> end
>
> However, now my data tip is not showing the correct information (Vector
> length and Mos) , and shows Error in Custom data tip function instead.
> When I remove the setappdata function it starts working again.

Set a breakpoint on the first line of the datatextbox function then attempt
to reproduce the error. Step through the function until it throws the error.
Determine why the error occurs and fix it.

Alternately, instead of using handles.figure1, use ANCESTOR on the Target
property of the event_obj object to retrieve its figure ancestor.

http://www.mathworks.com/help/matlab/ref/datacursormode.html

    "Handle of the object the data cursor is referencing (the object which
you click, for example, a line or a bar from a series)"

You can then set its application data or use GUIDATA to share whatever
information you want.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us