File Exchange Pick of the Week

May 30th, 2008

Dual interactive (and useful!) cursors

I love the power and extensibility of MATLAB. Want to create a line you can drag across your plot? It's not so hard to create one. (In fact, our very own Doug recently posted a nice video showing how you might create a line that follows the motion of the cursor.) Want more than one draggable line? Perhaps with data tips? Then want to be able to crop and export your plots based on where you drag those lines? Easily create your own utility, OR simply download Scott Hirsch's DUALCURSOR. DUALCURSOR is Brett's Pick as this week's featured function.

Scott elegantly wrote of his function: "A picture's worth a thousand words." In this case, though, a picture doesn't do justice to the words (code). Rather than try to show you via screen captures the utility of so interactive a function, I'm going to encourage you to download Scott's file and play around with it. You'll see how easy it is to reposition the dual cursors, and how the data tips update as you drag the lines. Then, with a right-click, you'll see how you can easily extract (to the workspace or to a new figure) the portion of your data enclosed by the cursors.

Contents

Load...

This creates variables Fs and y in your current workspace

load handel

...analyze...

Ns = 2^12;
Y = fft(y,Ns);
Y = 2/Ns*abs(Y(1:Ns/2));
df = Fs/Ns;
f = (0:1:Ns/2-1)*df;

...and visual some data

figure;
plot(f,100*sqrt(Y))
title('My cursor example');
xlabel('Frequency (Hz)');
ylabel('Amplitude');
axis([0 1200 0 inf]);

Now turn on dual cursors and start playing!

dualcursor
% Note: Make sure you try right-clicking and exporting sub-portions of your
% plot. Also, try dragging the datatip labels (which show the dx and dy
% values) around. Very nice!

Just for fun...

Might as well give the data a listen:

player = audioplayer(y, Fs);
play(player,[1 (get(player, 'SampleRate')*3)]);


Get the MATLAB code

Published with MATLAB® 7.6

2 Responses to “Dual interactive (and useful!) cursors”

  1. vincent replied on :

    this is a very nice feature guys, my compliments..
    I am currently developing a data acquisition post processor GUI and this would be very helpful, thanks!!

    I was only wondering how you have to change the file in order for the yellow blocks (with the x and y values in it) to stay at one defined place in stead of moving with the line?

    keep up the very good work!!

    cheers,

    Vincent

  2. Scott Hirsch replied on :

    Vincent -

    Thanks for your kind notes on my submission - I’m glad you find it useful. To answer your question - look through the code for references to textstring1 and textstring2 (you should find them in the ‘on’ and ‘update’ cases). In the ‘on’ case, replace xv1 and yv1 with the location you want (you’ll likely want to use normalized units, so that the location doesn’t depend on current axis limits). In the ‘update’ case, just remove the update to Position of the text objects. I’d love to see your updates posted back to the File Exchange, so others can benefit from your work.

    -scott

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • Zach: Hi Doug and Les, I didn’t have a lot of time to mess with this, but I did find a work-around. I plotted...
  • hamed: k
  • Les: @Zach This isn’t exactly what you are looking for but at least it puts all three parameters on the same...
  • Zach: Thanks for your suggestions Doug. I’ll give that a shot and see what happens. I’ve seen many of...
  • Doug: @Zach, I would say to use plotYYY, because that is close to what you want, but using depth as Y makes sense....
  • Doug: @Teja, I think this will work: http://www.mathworks .com/access/helpdesk /help/techdoc/ref...
  • Gify: merry christmas :) nice christmas tree! Regards, Janet Gify
  • Teja: Dear Doug Is there anyway to plot a surface from nonuniform data without meshgrid and griddata? Basically i...
  • Zach: I’m working with geophysical data, so I’d like to produce a depth profile. The y-axis would be...
  • Doug: @Ashok First, please do not use variable names that are MATLAB commands (std and mean). Second, p(j) should be...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.