{"id":2235,"date":"2008-05-30T10:43:50","date_gmt":"2008-05-30T15:43:50","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2008\/05\/30\/dual-interactive-and-useful-cursors\/"},"modified":"2016-11-17T10:25:37","modified_gmt":"2016-11-17T15:25:37","slug":"dual-interactive-and-useful-cursors","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2008\/05\/30\/dual-interactive-and-useful-cursors\/","title":{"rendered":"Dual interactive (and useful!) cursors"},"content":{"rendered":"<div class=\"content\">\n<p>I love the power and extensibility of MATLAB. Want to create a line you can drag across your plot? It&#8217;s not so hard to create<br \/>\none. (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<br \/>\ndata tips? Then want to be able to crop and export your plots based on where you drag those lines? Easily create your own<br \/>\nutility, OR simply download Scott Hirsch&#8217;s <a title=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=2875&amp;objectType=FILE (link no longer works)\">DUALCURSOR<\/a>. DUALCURSOR is Brett&#8217;s Pick as this week&#8217;s featured function.<\/p>\n<p>Scott elegantly wrote of his function: &#8220;A picture&#8217;s worth a thousand words.&#8221; In this case, though, a picture doesn&#8217;t do justice<br \/>\nto the words (code). Rather than try to show you via screen captures the utility of so interactive a function, I&#8217;m going to<br \/>\nencourage you to download Scott&#8217;s file and play around with it. You&#8217;ll see how easy it is to reposition the dual cursors,<br \/>\nand how the data tips update as you drag the lines. Then, with a right-click, you&#8217;ll see how you can easily extract (to the workspace or<br \/>\nto a new figure) the portion of your data enclosed by the cursors.<\/p>\n<p>&nbsp;<\/p>\n<h3>Contents<\/h3>\n<div>\n<ul>\n<li><a href=\"#1\">Load&#8230;<\/a><\/li>\n<li><a href=\"#2\">&#8230;analyze&#8230;<\/a><\/li>\n<li><a href=\"#3\">&#8230;and visual some data<\/a><\/li>\n<li><a href=\"#4\">Now turn on dual cursors and start playing!<\/a><\/li>\n<li><a href=\"#5\">Just for fun&#8230;<\/a><\/li>\n<\/ul>\n<\/div>\n<h3>Load&#8230;<a name=\"1\"><\/a><\/h3>\n<p>This creates variables Fs and y in your current workspace<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">load <span style=\"color: #a020f0;\">handel<\/span><\/pre>\n<h3>&#8230;analyze&#8230;<a name=\"2\"><\/a><\/h3>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">Ns = 2^12;\r\nY = fft(y,Ns);\r\nY = 2\/Ns*abs(Y(1:Ns\/2));\r\ndf = Fs\/Ns;\r\nf = (0:1:Ns\/2-1)*df;<\/pre>\n<h3>&#8230;and visual some data<a name=\"3\"><\/a><\/h3>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">figure;\r\nplot(f,100*sqrt(Y))\r\ntitle(<span style=\"color: #a020f0;\">'My cursor example'<\/span>);\r\nxlabel(<span style=\"color: #a020f0;\">'Frequency (Hz)'<\/span>);\r\nylabel(<span style=\"color: #a020f0;\">'Amplitude'<\/span>);\r\naxis([0 1200 0 inf]);<\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/dualcursor_potw_01.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/><\/p>\n<h3>Now turn on dual cursors and start playing!<a name=\"4\"><\/a><\/h3>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">dualcursor\r\n<span style=\"color: #228b22;\">% Note: Make sure you try right-clicking and exporting sub-portions of your<\/span>\r\n<span style=\"color: #228b22;\">% plot. Also, try dragging the datatip labels (which show the dx and dy<\/span>\r\n<span style=\"color: #228b22;\">% values) around. Very nice!<\/span><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/dualcursor_potw_02.png\" alt=\"\" hspace=\"5\" vspace=\"5\" \/><\/p>\n<h3>Just for fun&#8230;<a name=\"5\"><\/a><\/h3>\n<p>Might as well give the data a listen:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid #c8c8c8;\">player = audioplayer(y, Fs);\r\nplay(player,[1 (get(player, <span style=\"color: #a020f0;\">'SampleRate'<\/span>)*3)]);<\/pre>\n<p><script>\/\/ <![CDATA[\nfunction grabCode_cd6cd94fe7634228876ea4be14078be9() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='cd6cd94fe7634228876ea4be14078be9 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' cd6cd94fe7634228876ea4be14078be9';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        author = 'Brett Shoelson';\n        copyright = 'Copyright 2008 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n\n\n\n\n<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\n\n\n\n\n\n\n\\n');\n      \n      d.title = title + ' (MATLAB code)';\n      d.close();\n      }\n\/\/ ]]><\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight: lighter; font-style: italic; color: gray;\"><a><span style=\"font-size: x-small; font-style: italic;\">Get<br \/>\nthe MATLAB code<br \/>\n<noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p>Published with MATLAB\u00ae 7.6<\/p>\n<\/div>\n<p><!--\ncd6cd94fe7634228876ea4be14078be9 ##### SOURCE BEGIN #####\n%% DUALCURSOR\n% I love the power and extensibility of MATLAB. Want to create a line you\n% can drag across your plot? It's not so hard to create one. (In fact, our\n% very own\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadAuthor.do?objectId=982174&objectType=author Doug>\n% recently posted a <https:\/\/blogs.mathworks.com\/pick\/2008\/05\/27\/advanced-matlab-capture-mouse-movement\/ nice video>\n% showing how you might create a line that follows the motion of the cursor.) Want more than one draggable line? Perhaps with\n% 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\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadAuthor.do?objectType=author&objectId=919425 Scott Hirsch>'s <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=2875&objectType=FILE DUALCURSOR>. DUALCURSOR is <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadAuthor.do?objectId=1093599&objectType=author Brett>'s Pick as this week's featured function.\n%\n% Scott elegantly wrote of his function: \"A picture's worth a thousand\n% words.\" In this case, though, a picture doesn't do justice to the words\n% (code). Rather than try to show you via screen captures the utility of so\n% interactive a function, I'm going to encourage you to download Scott's\n% file and play around with it. You'll see how easy it is to reposition the\n% dual cursors, and how the data tips update as you drag the lines. Then, with\n% a right-click, you can easily extract (to the workspace or to a new\n% figure) the portion of your data enclosed by the cursors.\n\n%% Load...\n% This creates variables Fs and y in your current workspace\nload handel\n%% ...analyze...\nNs = 2^12;\nY = fft(y,Ns);\nY = 2\/Ns*abs(Y(1:Ns\/2));\ndf = Fs\/Ns;\nf = (0:1:Ns\/2-1)*df;\n%% ...and visual some data\nfigure;\nplot(f,100*sqrt(Y))\ntitle('My cursor example');\nxlabel('Frequency (Hz)');\nylabel('Amplitude');\naxis([0 1200 0 inf]);\n%% Now turn on dual cursors and start playing!\ndualcursor\n% Note: Make sure you try right-clicking and exporting sub-portions of your\n% plot. Also, try dragging the datatip labels (which show the dx and dy\n% values) around. Very nice!\n\n%% Just for fun...\n% Might as well give the data a listen:\nplayer = audioplayer(y, Fs);\nplay(player,[1 (get(player, 'SampleRate')*3)]);\n##### SOURCE END ##### cd6cd94fe7634228876ea4be14078be9\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\nI love the power and extensibility of MATLAB. Want to create a line you can drag across your plot? It&#8217;s not so hard to create<br \/>\none. (In fact, our very own Doug recently posted a nice video&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2008\/05\/30\/dual-interactive-and-useful-cursors\/\">read more >><\/a><\/p>\n","protected":false},"author":34,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2235"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2235"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2235\/revisions"}],"predecessor-version":[{"id":8058,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2235\/revisions\/8058"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}