# 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

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)]);

Published with MATLAB® 7.6

|