{"id":95,"date":"2006-11-21T07:00:52","date_gmt":"2006-11-21T12:00:52","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=95"},"modified":"2019-10-22T16:33:24","modified_gmt":"2019-10-22T20:33:24","slug":"old-habits","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2006\/11\/21\/old-habits\/","title":{"rendered":"Old habits"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/blogs.mathworks.com\/steve\/?p=94\">Last time<\/a> I showed off custom data cursors with a little utility function that displays object labels as data cursors:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/94\/data_cursor_screenshot3.png\"> <\/p>\r\n   <p>My code used <tt>label2rgb<\/tt> to display a \"colorized\" label matrix.  The custom data cursor callback, though, needed the original label matrix, so I used\r\n      <tt>setappdata<\/tt> to save the label matrix in a place where the callback routine could retrieve it.\r\n   <\/p>\r\n   <p>Within a few hours of posting that code, I had a \"Doh!\" moment.  I haven't used that kind of <tt>setappdata<\/tt> trick since nested function handles appeared in MATLAB 7.0.  I have no idea why I suddenly reverted to my previous coding\r\n      patterns.  Old habits die hard, as the cliche says.\r\n   <\/p>\r\n   <p>Anyway, nested function handles in MATLAB 7 can be used to create callback functions that carry around their own state.  I\r\n      thought I would show you how my <a href=\"https:\/\/blogs.mathworks.com\/images\/steve\/94\/display_label_matrix.m\"><tt>display_label_matrix<\/tt><\/a> function looks using this technique.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">type <span style=\"color: #A020F0\">display_label_matrix<\/span><\/pre><pre style=\"font-style:oblique\">\r\nfunction display_label_matrix(L)\r\n% display_label_matrix Display label matrix with custom data cursors\r\n% display_label_matrix(L) displays the label matrix L as colored objects on\r\n% a gray background.  It also installs a custom data cursor handler in the\r\n% figure.  In data cursor mode, clicking on an object displays a data\r\n% cursor containing the object's number.\r\n\r\n% Steve Eddins\r\n% Copyright 2006 The MathWorks, Inc.\r\n% $Revision: 1.2 $  $Date: 2006\/11\/21 02:41:00 $\r\n\r\nrgb = label2rgb(L, 'jet', [.7 .7 .7], 'shuffle');\r\nh = imshow(rgb);\r\n\r\n% Enable the figure data cursor mode, and use our own custom data cursor\r\n% strings.\r\ndcm = datacursormode(ancestor(h, 'figure'));\r\nset(dcm,'Enable','on', ...\r\n    'Updatefcn', @dataCursorText);\r\n\r\n    %======================================================================\r\n    function output_text = dataCursorText(obj, event_obj)\r\n    % Respond to a user click in data cursor mode by displaying the label\r\n    % number of the pixel that was clicked on.\r\n\r\n        h_image = get(event_obj, 'Target');\r\n        pos = get(event_obj, 'Position');\r\n        clicked_label = L(round(pos(2)), round(pos(1)));\r\n        output_text = sprintf('%d', clicked_label);\r\n    end\r\n    %----------------------------------------------------------------------\r\n    \r\nend\r\n\r\n\r\n\r\n\r\n<\/pre><p>In the new version, the callback function, <tt>dataCursorText<\/tt>, is nested within the outer function, <tt>display_label_matrix<\/tt>.  Notice how <tt>dataCursorText<\/tt> uses a variable, <tt>L<\/tt>, that is created in the scope of the outer function, <tt>display_label_matrix<\/tt>.  When the outer function creates a function handle to <tt>dataCursorText<\/tt>, the value of <tt>L<\/tt> at that moment is captured into a kind of private workspace that goes along with the function handle.  No auxiliary information\r\n      or state needs to be stored anywhere else; the function handle has inside of it all the information it needs to perform its\r\n      computation.\r\n   <\/p>\r\n   <p>If you are interested in learning more about nested functions in MATLAB, you might want to look at <a href=\"https:\/\/www.mathworks.com\/company\/newsletters\/articles\/programming-patterns-nested-functions-in-matlab.html\">Loren Shure's News &amp; Notes article<\/a> on the topic.\r\n   <\/p><script language=\"JavaScript\"> \r\n<!--\r\n    function grabCode_4ce884f48fbc441bb17ef96dc2430b85() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='4ce884f48fbc441bb17ef96dc2430b85 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 4ce884f48fbc441bb17ef96dc2430b85';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Steve Eddins';\r\n        copyright = 'Copyright 2006 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<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');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }  \r\n--> \r\n      <\/script>\r\n<noscript>\r\n<em>A JavaScript-enabled browser is required to use the \"Get the MATLAB code\" link.<\/em>\r\n<\/noscript>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_4ce884f48fbc441bb17ef96dc2430b85()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code<\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.3<br><\/p>\r\n<\/div>\r\n<!--\r\n4ce884f48fbc441bb17ef96dc2430b85 ##### SOURCE BEGIN #####\r\n%% Old Habits\r\n% <https:\/\/blogs.mathworks.com\/steve\/?p=94 Last time> I showed off custom \r\n% data cursors with a little utility function\r\n% that displays object labels as data cursors:\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/94\/data_cursor_screenshot3.png>>\r\n%\r\n% My code used |label2rgb| to display a \"colorized\" label matrix.  The\r\n% custom data cursor callback, though, needed the original label matrix, so\r\n% I used |setappdata| to save the label matrix in a place where the\r\n% callback routine could retrieve it.\r\n%\r\n% Within a few hours of posting that code, I had a \"Doh!\" moment.  I\r\n% haven't used that kind of |setappdata| trick since nested function\r\n% handles appeared in MATLAB 7.0.  I have no idea why I suddenly reverted\r\n% to my previous coding patterns.  Old habits die hard, as the cliche says.\r\n%\r\n% Anyway, nested function handles in MATLAB 7 can be used to create\r\n% callback functions that carry around their own state.  I thought I would\r\n% show you how my <https:\/\/blogs.mathworks.com\/images\/steve\/94\/display_label_matrix.m \r\n% |display_label_matrix|> function looks using this technique.\r\n\r\ntype display_label_matrix\r\n\r\n%%\r\n% In the new version, the callback function, |dataCursorText|, is nested\r\n% within the outer function, |display_label_matrix|.  Notice how\r\n% |dataCursorText| uses a variable, |L|, that is created in the scope of\r\n% the outer function, |display_label_matrix|.  When the outer function\r\n% creates a function handle to |dataCursorText|, the value of |L| at that\r\n% moment is captured into a kind of private workspace that goes along with\r\n% the function handle.  No auxiliary information or state needs to be\r\n% stored anywhere else; the function handle has inside of it all the\r\n% information it needs to perform its computation.\r\n%\r\n% If you are interested in learning more about nested functions in MATLAB,\r\n% you might want to look at <https:\/\/www.mathworks.com\/company\/newsletters\/articles\/programming-patterns-nested-functions-in-matlab.html \r\n% Loren Shure's News & Notes article> on the topic.\r\n##### SOURCE END ##### 4ce884f48fbc441bb17ef96dc2430b85\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Last time I showed off custom data cursors with a little utility function that displays object labels as data cursors:\r\n   \r\n    \r\n   My code used label2rgb to display a \"colorized\" label... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/11\/21\/old-habits\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[272,266,36,152,188,270],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/95"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=95"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/95\/revisions"}],"predecessor-version":[{"id":3514,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/95\/revisions\/3514"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}