Quantcast

Data cursor in Octave

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Data cursor in Octave

roland65
Hi there,

is there a way in Octave to get a data cursor like the datatip in Matlab ?

I know I can use the ginput() function but I'd like to get the coordinates of a point of the data series and not of any point in the figure...

Thanks,
RB
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Data cursor in Octave

Pantxo
There is nothing like this currently in Octave but it had been a while since I wanted to give it a try.
I attached a draft function that should do what you want. It only works when there is a single axes in the figure and only points to line objects:

plot (rand (20, 4)
h = datacursor ();

%% get the coordinates of the data cursor
x = get (h, "x")(2);
y = get (h, "y")(2);


Pantxo


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

datacursor.m (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Data cursor in Octave

Pantxo


2017-02-09 13:16 GMT+01:00 Pantxo Diribarne <[hidden email]>:
There is nothing like this currently in Octave but it had been a while since I wanted to give it a try.
I attached a draft function that should do what you want. It only works when there is a single axes in the figure and only points to line objects:

plot (rand (20, 4)
h = datacursor ();

%% get the coordinates of the data cursor
x = get (h, "x")(2);
y = get (h, "y")(2);


Pantxo

I attached an updated version, the previous one had problems in the displayed coordinates and didn't handle log coordinates.

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

datacursor.m (4K) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Data cursor in Octave

roland65
Thanks a lot ! This exactly what I was looking for...

It works well with the fltk and qt graphics toolkits.

However, there is a minor issue with the qt toolkit: when using the datacursor() function, the coordinates that are displayed at the bottom of the plot become normalized between 0 and 1, and thus are wrong.

I found that this issue is related to the annotation() function you use. So it is easly to reproduce the issue like this (with the qt toolkit) :

% Plot a sinus ; the display coordinates are OK
close all; clear all; x=[0:0.01:2*pi]; y=sin(x);plot(x,y);

% Annotate the figure ; the display coordinates are between 0 and 1
annotation("textbox",[0.5 0.5 0 0],"string","TEXT")

Note the issue is also present when using the "Insert text" menu.

Perhaps I shall fill a bug report, or is it a known issue ?

Anyway, many thanks for your very useful function.
RB
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Data cursor in Octave

Pantxo
roland65 wrote
However, there is a minor issue with the qt toolkit: when using the datacursor() function, the coordinates that are displayed at the bottom of the plot become normalized between 0 and 1, and thus are wrong
....

Perhaps I shall fill a bug report, or is it a known issue ?

RB
Yes please do so. The coordinates you see are those of the annotation transparent axes, which is above any other axes but should not be taken into account when displaying the coordinates of the mouse cursor in the status bar.



Pantxo
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Data cursor in Octave

roland65
Pantxo wrote
Yes please do so.
OK, I filled a bug report (https://savannah.gnu.org/bugs/index.php?50272).

Another question: would it be difficult to add a menu that calls the datacursor() function to the figure window? I looked at the source code, but I couldn't understand how to call an Octave function from within the C++.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Data cursor in Octave

Pantxo
roland65 wrote
Pantxo wrote
Yes please do so.
OK, I filled a bug report (https://savannah.gnu.org/bugs/index.php?50272).

Another question: would it be difficult to add a menu that calls the datacursor() function to the figure window? I looked at the source code, but I couldn't understand how to call an Octave function from within the C++.
See [1] for how to call m code from c++, basically with feval function.
It is thus not very difficult, but it has to be implemented for both fltk and Qt.

As a temporary workaround you can add the menu yourself. See uimenu.



[1] https://www.gnu.org/software/octave/doc/interpreter/Calling-Octave-Functions-from-Oct_002dFiles.html#Calling-Octave-Functions-from-Oct_002dFiles
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Data cursor in Octave

roland65
Pantxo wrote
It is thus not very difficult, but it has to be implemented for both fltk and Qt.
OK, thanks for the tips, it helped me a lot.

I tried to add a menu to the qt figure. It almost works. Indeed, when I click on my custom "Data cursor" menu item, the datacursor() function is executed (I see the coordinates that changes at the bottom) but the annotation is not shown. For the whole thing to work, I have to go to the Octave terminal and press the enter key once. Then, the datacursor works as expected.

Do you have any idea about this strange behaviour?

Here is my code (in libgui/graphics/Figure.cc):


void
Figure::createFigureToolBarAndMenuBar (void)
{

   <...>

  editMenu->addSeparator ();
  editMenu->addActions (m_mouseModeGroup->actions ());
  editMenu->addAction (tr ("&Data cursor"), this, SLOT (dataCursor (void)));

  < ... >
}

void
Figure::dataCursor (void)
{
    octave_link::post_event (this, &Figure::datacursor_callback);
}

void
Figure::datacursor_callback (void)
{
  figure::properties& fp = properties<figure> ();
  octave_value fnum = fp.get___myhandle__ ().as_octave_value ();

  Ffeval (ovl ("datacursor", fnum));

  //redraw (); // Needed ?
}


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Data cursor in Octave

Pantxo
In reply to this post by Pantxo

I went the .m file/uimenu route and it seems to works correctly. I attched the preliminary files. Usage:

plot (rand (20, 4))
datacursor_menu

Pantxo

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

datacursor.m (4K) Download Attachment
datacursor_menu.m (3K) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Data cursor in Octave

roland65
Pantxo wrote
I went the .m file/uimenu route and it seems to works correctly. I attched
the preliminary files.
Nice! It works as expected, thanks a lot!

Anyway, I'd like to understand what goes wrong in my code. I tried to replace the feval(ovl(datacursor...) code with a call to the disp() function and the behaviour is the same. I have to press the return key, otherwise the disp() function gets stuck.

Do you think I'll have to fill a bug report again, or did I miss something ?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Data cursor in Octave

Pantxo
roland65 wrote
Pantxo wrote
I went the .m file/uimenu route and it seems to works correctly. I attched
the preliminary files.
Nice! It works as expected, thanks a lot!

Anyway, I'd like to understand what goes wrong in my code. I tried to replace the feval(ovl(datacursor...) code with a call to the disp() function and the behaviour is the same. I have to press the return key, otherwise the disp() function gets stuck.

Do you think I'll have to fill a bug report again, or did I miss something ?
I remember having seen such behavior reported (need pressing <enter> in the command window for plotting to proceed) but I fail to find the original report. Can you file a bug report.

Pantxo
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Data cursor in Octave

roland65
Pantxo wrote
Can you file a bug report
Done! It's here: http://savannah.gnu.org/bugs/?50295

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Data cursor in Octave

roland65
In reply to this post by Pantxo
Pantxo wrote
I went the .m file/uimenu route and it seems to works correctly. I attched
the preliminary files.
I found an issue in the datacursor.m function. Indeed, if you have two figures (1 and 2) and figure 2 is the current figure, then datacursor(1) displays the cursor on the current figure (figure 2), not on figure 1.

I guess you simply forgot to add the figure handle to the annotation() function call. So I attach the fixed datacursor.m function.

datacursor.m
Loading...