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



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