{"id":239,"date":"2010-07-09T18:24:59","date_gmt":"2010-07-09T18:24:59","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2010\/07\/09\/scalar-roulette\/"},"modified":"2010-06-30T18:25:56","modified_gmt":"2010-06-30T18:25:56","slug":"scalar-roulette","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2010\/07\/09\/scalar-roulette\/","title":{"rendered":"Scalar Roulette"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>A while ago, Steve posted an article entitled <a href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/08\/31\/functional-design-clunkers\/\">Functional Design Clunkers<\/a> on his blog.  Well, I have a confession to make too.  Have you ever been bitten by the ambiguity in the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/contour.html\"><tt>contour<\/tt><\/a> function?\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Two Syntaxes That Can Clash<\/a><\/li>\r\n         <li><a href=\"#3\">Possible Solutions<\/a><\/li>\r\n         <li><a href=\"#4\">Have You Been Bitten?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Two Syntaxes That Can Clash<a name=\"1\"><\/a><\/h3>\r\n   <p>Here are the two syntaxes that can class for <tt>contour<\/tt>:\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li><tt>contour(Z,n)<\/tt><\/li>\r\n         <li><tt>contour(Z,v)<\/tt><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>From the documentation:<\/p>\r\n   <p><i><tt>contour(Z,v)<\/tt> draws a contour plot of matrix <tt>Z<\/tt> with contour lines at the data values specified in the monotonically increasing vector <tt>v<\/tt>. The number of contour levels is equal to <tt>length(v)<\/tt>. To draw a single contour of level <tt>i<\/tt>, use <tt>contour(Z,[i i])<\/tt>.<\/i><\/p>\r\n   <p>Time for the confession.  I thought at the time we were designing <tt>contour<\/tt> that asking for a single contour would be rare enough to warrant having the \"convenient\" syntaxes that could clash.  And\r\n      I even know the work-around for programmers.  If they wanted to specify the contour levels, they simply needed some code like\r\n      this:\r\n   <\/p><pre>         if length(mycontours) == 1\r\n            mycontours(2) = mycontours;\r\n         end\r\n         % now call the contour function<\/pre><p>So, why don't I like this now?  The code is harder to read, the intent is harder to discern, and it just feels clunky.<\/p>\r\n   <h3>Possible Solutions<a name=\"3\"><\/a><\/h3>\r\n   <p>I can think of a bunch of possible solutions, were we to design the <tt>contour<\/tt> function today.  These include (but certainly aren't limited to):\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>two functions, 1 for number of contours, 2 for values of contours<\/li>\r\n         <li>control number vs. level behavior with param\/value pairs or some similar device<\/li>\r\n         <li>have one of the inputs (perhaps scalar level number) be in a cell array or some other class that differs from the class for\r\n            the level values input\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Have You Been Bitten?<a name=\"4\"><\/a><\/h3>\r\n   <p>Has the ambiguity in <tt>contour<\/tt> bitten you?  Or one in some other MATLAB functionality?  What solution do you prefer?  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=239#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_ea00c8618bd0421298dc7ddd3381a0f3() {\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='ea00c8618bd0421298dc7ddd3381a0f3 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' ea00c8618bd0421298dc7ddd3381a0f3';\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 = 'Loren Shure';\r\n        copyright = 'Copyright 2010 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_ea00c8618bd0421298dc7ddd3381a0f3()\"><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; 7.10<br><\/p>\r\n<\/div>\r\n<!--\r\nea00c8618bd0421298dc7ddd3381a0f3 ##### SOURCE BEGIN #####\r\n%% Scalar Roulette\r\n% A while ago, Steve posted an article entitled \r\n% <https:\/\/blogs.mathworks.com\/steve\/2009\/08\/31\/functional-design-clunkers\/ \r\n% Functional Design Clunkers> on his blog.  Well, I have a confession to\r\n% make too.  Have you ever been bitten by the ambiguity in the \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010a\/techdoc\/ref\/contour.html\r\n% |contour|> function?\r\n%\r\n%% Two Syntaxes That Can Clash\r\n% Here are the two syntaxes that can class for |contour|:\r\n%\r\n% * |contour(Z,n)|\r\n% * |contour(Z,v)|\r\n%\r\n% From the documentation:\r\n%\r\n% _|contour(Z,v)| draws a contour plot of matrix |Z| with contour lines at the\r\n% data values specified in the monotonically increasing vector |v|. The\r\n% number of contour levels is equal to |length(v)|. To draw a single contour\r\n% of level |i|, use |contour(Z,[i i])|._\r\n%%\r\n% Time for the confession.  I thought at the time we were designing\r\n% |contour| that asking for a single contour would be rare enough to\r\n% warrant having the \"convenient\" syntaxes that could clash.  And I even\r\n% know the work-around for programmers.  If they wanted to specify the\r\n% contour levels, they simply needed some code like this:\r\n%\r\n%           if length(mycontours) == 1\r\n%              mycontours(2) = mycontours;\r\n%           end\r\n%           % now call the contour function\r\n%\r\n% So, why don't I like this now?  The code is harder to read, the intent is\r\n% harder to discern, and it just feels clunky.\r\n%% Possible Solutions\r\n% I can think of a bunch of possible solutions, were we to design the\r\n% |contour| function today.  These include (but certainly aren't limited\r\n% to):\r\n%\r\n% * two functions, 1 for number of contours, 2 for values of contours\r\n% * control number vs. level behavior with param\/value pairs or some\r\n% similar device\r\n% * have one of the inputs (perhaps scalar level number) be in a cell array\r\n% or some other class that differs from the class for the level values\r\n% input\r\n%% Have You Been Bitten?\r\n% Has the ambiguity in |contour| bitten you?  Or one in some other MATLAB\r\n% functionality?  What solution do you prefer?  Let me know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=239#respond here>.\r\n\r\n\r\n##### SOURCE END ##### ea00c8618bd0421298dc7ddd3381a0f3\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      A while ago, Steve posted an article entitled Functional Design Clunkers on his blog.  Well, I have a confession to make too.  Have you ever been bitten by the ambiguity in the contour... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2010\/07\/09\/scalar-roulette\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/239"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=239"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/239\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}