{"id":12126,"date":"2021-02-19T13:31:58","date_gmt":"2021-02-19T18:31:58","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=12126"},"modified":"2021-02-19T13:31:58","modified_gmt":"2021-02-19T18:31:58","slug":"the-matrix-lab-has-you","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2021\/02\/19\/the-matrix-lab-has-you\/","title":{"rendered":"The Matrix Lab Has You"},"content":{"rendered":"<div xmlns:mwsh=\"http:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>'s pick this week is <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/72669\">The Matrix lab has you<\/a> by <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/8668631\">Mathworlds<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <p>I discovered this after reading <a href=\"https:\/\/blogs.mathworks.com\/pick\/2021\/02\/09\/tracing-the-boundary-of-a-binary-region-in-an-image\/\">Brett's post<\/a> a couple weeks ago.  It's a fun submission that you guys can run yourself - I won't spoil it.  This submission does rely\r\n      on some undocumented capabilities that <i>will<\/i> break in the future (some already have).  But there will be replacements for those, some already available.\r\n   <\/p>\r\n   <p>The thing that caught my eye about this submission was the word of warning on the FEX page: <i>NB : Running this programs clears your Matlab console and may also change some of your Matlab preferences (font and background\r\n         colors) <b>if interrupted while running<\/b>.<\/i><\/p>\r\n   <p>The fact that it doesn't reset if interrupted tells me there is improvement for how this function should tear itself down.\r\n       Right now it:\r\n   <\/p>\r\n   <div>\r\n      <ol>\r\n         <li>Gets the original settings values<\/li>\r\n         <li>Sets new settings values<\/li>\r\n         <li>Runs the demo<\/li>\r\n         <li>Sets the settings back to the original value<\/li>\r\n      <\/ol>\r\n   <\/div>\r\n   <p>So if you break with ctrl+c or an error occurs, [4] never gets to run. There's a better way to do this using <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/oncleanup.html\"><tt>onCleanup<\/tt><\/a> objects.  These reliably fire when the object is cleared regardless of if there is an error!\r\n   <\/p>\r\n   <p>I'll demonstrate by changing the command window error color for this MATLAB session using the new documented <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_external\/access-and-modify-settings.html\">settings API<\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s = settings;\r\ns.matlab.colors.commandwindow.ErrorColor.TemporaryValue = [0 0 255] <span style=\"color: #228B22\">% blue errors<\/span>\r\ncolorresetter = onCleanup(@()s.matlab.colors.commandwindow.ErrorColor.clearTemporaryValue())<\/pre><pre style=\"font-style:oblique\">s = \r\n  SettingsGroup with properties:\r\n                slhistory: [1&times;1 SettingsGroup]\r\n                 Simulink: [1&times;1 SettingsGroup]\r\n                   matlab: [1&times;1 SettingsGroup]\r\ncolorresetter = \r\n  onCleanup with properties:\r\n\r\n    task: @()s.matlab.colors.commandwindow.ErrorColor.clearTemporaryValue()\r\n<\/pre><p>Now when the <i>colorresetter<\/i> variable is cleared, it will automatically clear the value that I'd set.  In a function, this would happen implicitly at\r\n      the end.  So the fact that this error occurs does not mean I'm permanently stuck or need to manually adjust the setting.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">error(<span style=\"color: #A020F0\">'smurf:error'<\/span>, <span style=\"color: #A020F0\">'This error message is blue'<\/span>)<\/pre><pre style=\"font-style:oblique; color:#0000FF\">Error using mainMatrixLabHasYou (line 52)\r\nThis error message is blue<\/pre><p>I use <tt>onCleanup<\/tt> objects all of the time.  Some frequent things I'll use them for:\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>Changing a setting<\/li>\r\n         <li><tt>fclose(fid)<\/tt><\/li>\r\n         <li>Changing directories to somewhere else<\/li>\r\n         <li>Deleting a temporary file or folder I created<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>In the scope of unit testing, you should use the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.unittest.testcase.addteardown.html\"><tt>testCase.addTeardown<\/tt><\/a> method which will also reliably fire a task but do it in the right phase of the test execution.\r\n   <\/p>\r\n   <h3>Comments<a name=\"4\"><\/a><\/h3>\r\n   <p>Give it a try and let us know what you think <a href=\"http:\/\/blogs.mathworks.com\/pick\/?p=12126#respond\">here<\/a> or leave a <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/72669#comments\">comment<\/a> for Mathworlds.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_7ea8480488cf487dbf3f82385aa7e5f9() {\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='7ea8480488cf487dbf3f82385aa7e5f9 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 7ea8480488cf487dbf3f82385aa7e5f9';\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 = 'Sean de Wolski';\r\n        copyright = 'Copyright 2021 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-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_7ea8480488cf487dbf3f82385aa7e5f9()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2020b<br><\/p>\r\n<\/div>\r\n<!--\r\n7ea8480488cf487dbf3f82385aa7e5f9 ##### SOURCE BEGIN #####\r\n%% The Matrix Lab has you\r\n%\r\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s pick this week is\r\n% <http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/72669 The Matrix lab has you> by\r\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/8668631 Mathworlds>.\r\n% \r\n\r\n%% \r\n%\r\n% I discovered this after reading\r\n% <https:\/\/blogs.mathworks.com\/pick\/2021\/02\/09\/tracing-the-boundary-of-a-binary-region-in-an-image\/\r\n% Brett's post> a couple weeks ago.  It's a fun submission that you guys\r\n% can run yourself - I won't spoil it.  This submission does rely on some\r\n% undocumented capabilities that _will_ break in the future (some already\r\n% have).  But there will be replacements for those, some already available.\r\n%\r\n% The thing that caught my eye about this submission was the word of\r\n% warning on the FEX page: _NB : Running this programs clears your Matlab\r\n% console and may also change some of your Matlab preferences (font and\r\n% background colors) *if interrupted while running*._\r\n%\r\n% The fact that it doesn't reset if interrupted tells me there is\r\n% improvement for how this function should tear itself down.  Right now it:\r\n%\r\n% # Gets the original settings values\r\n% # Sets new settings values\r\n% # Runs the demo\r\n% # Sets the settings back to the original value\r\n% \r\n% So if you break with ctrl+c or an error occurs, [4] never gets to run.\r\n% There's a better way to do this using\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/oncleanup.html |onCleanup|>\r\n% objects.  These reliably fire when the object is cleared regardless of if\r\n% there is an error!  \r\n%\r\n% I'll demonstrate by changing the command window error\r\n% color for this MATLAB session using the new documented\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_external\/access-and-modify-settings.html\r\n% settings API>.\r\n%\r\n\r\ns = settings;\r\ns.matlab.colors.commandwindow.ErrorColor.TemporaryValue = [0 0 255] % blue errors\r\ncolorresetter = onCleanup(@()s.matlab.colors.commandwindow.ErrorColor.clearTemporaryValue())\r\n\r\n%%\r\n% Now when the _colorresetter_ variable is cleared, it will automatically\r\n% clear the value that I'd set.  In a function, this would happen\r\n% implicitly at the end.  So the fact that this error occurs does not mean\r\n% I'm permanently stuck or need to manually adjust the setting.\r\n\r\nerror('smurf:error', 'This error message is blue')\r\n\r\n%%\r\n% I use |onCleanup| objects all of the time.  Some frequent things I'll use\r\n% them for:\r\n%\r\n% * Changing a setting\r\n% * |fclose(fid)|\r\n% * Changing directories to somewhere else\r\n% * Deleting a temporary file or folder I created\r\n%\r\n% In the scope of unit testing, you should use the\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.unittest.testcase.addteardown.html\r\n% |testCase.addTeardown|> method which will also reliably fire a task but\r\n% do it in the right phase of the test execution.\r\n\r\n\r\n%% Comments\r\n% \r\n% Give it a try and let us know what you think\r\n% <http:\/\/blogs.mathworks.com\/pick\/?p=12126#respond here> or leave a\r\n% <http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/72669#comments\r\n% comment> for Mathworlds.\r\n%\r\n \r\n\r\n##### SOURCE END ##### 7ea8480488cf487dbf3f82385aa7e5f9\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Sean's pick this week is The Matrix lab has you by Mathworlds.\r\n      \r\n   \r\n   I discovered this after reading Brett's post a couple weeks ago.  It's a fun submission that... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2021\/02\/19\/the-matrix-lab-has-you\/\">read more >><\/a><\/p>","protected":false},"author":87,"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\/12126"}],"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\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=12126"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/12126\/revisions"}],"predecessor-version":[{"id":12128,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/12126\/revisions\/12128"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=12126"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=12126"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=12126"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}