{"id":11042,"date":"2019-10-04T09:00:46","date_gmt":"2019-10-04T13:00:46","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=11042"},"modified":"2019-10-02T16:01:30","modified_gmt":"2019-10-02T20:01:30","slug":"leaf-pile","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2019\/10\/04\/leaf-pile\/","title":{"rendered":"Leaf Pile!"},"content":{"rendered":"<div xmlns:mwsh=\"http:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\n   <introduction><\/p>\n<p><a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>&#8216;s pick this week is <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/72880\"><tt>leafpile<\/tt><\/a> by <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/9069783\">Nathaniel Barlow<\/a>.\n      <\/p>\n<p>   <\/introduction><\/p>\n<p>As we&#8217;re <i>finally<\/i> starting to see cooler temperatures in New England, the leaves are starting to turn.  This fun submission allows MATLAB to make a pile of leaves for you to rake or have your virtual dog play in.\n   <\/p>\n<p>To play with this, I&#8217;m going to make a simple one line live script with controls to make it so I can play with the settings.  I&#8217;ll then hide the code so we can just play with the controls and make leaf piles.\n   <\/p>\n<p>\n   <embed src=\"https:\/\/blogs.mathworks.com\/pick\/files\/leafpile.mp4\" height=\"700\" width=\"700\"><br \/>\n   <\/p>\n<p>Now let&#8217;s do a quick code review of the input parsing step that is used in <tt>leafpile<\/tt>.  This is the relevant part:\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">function<\/span> leafpile(N,type)\r\n<span style=\"color: #228B22\">%leafpile(leaves,type) makes a random pile of N leaves<\/span>\r\n<span style=\"color: #228B22\">% of type 'oak' or 'maple'.  N. Barlow 9\/28\/19<\/span>\r\n<span style=\"color: #228B22\">%leafpile(N) makes a mixed pile of N leves.<\/span>\r\n<span style=\"color: #228B22\">%leafpile makes a pile of 70 mixed leaves.<\/span>\r\n<span style=\"color: #228B22\">%The mathematical functions describing the leaf shapes were created by Hamid Naderi<\/span>\r\n<span style=\"color: #228B22\">%Yeganeh and given at https:\/\/blogs.scientificamerican.com\/guest-blog\/how-to-draw-with-math\/<\/span>\r\n\r\n<span style=\"color: #0000FF\">if<\/span> nargin==0,N=70; type=<span style=\"color: #A020F0\">'mixed'<\/span>; <span style=\"color: #0000FF\">end<\/span>;\r\n<span style=\"color: #0000FF\">if<\/span> nargin==1, type=<span style=\"color: #A020F0\">'mixed'<\/span>; <span style=\"color: #0000FF\">end<\/span>;\r\n\r\n<span>&#8942;<\/span><\/pre>\n<p>Using the new <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2019b\/matlab\/ref\/arguments.html\"><span style=\"color: #0000FF\">arguments<\/span><\/a> construct that Jiro <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=11026\">wrote about<\/a> last week (my personal favorite R2019b feature) this could be rewritten with the following which would also validate the inputs:\n   <\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">function<\/span> leafpile(N,type)\r\n<span style=\"color: #228B22\">&#8942;<\/span>\r\n\r\n<span style=\"color: #0000FF\">arguments<\/span>\r\n    N(1,1) double {mustBeInteger, mustBePositive} = 70;\r\n    type {mustBeMember(type, {'mixed', 'maple', 'oak'})} = <span style=\"color: #A020F0\">'mixed'<\/span>;\r\n<span style=\"color: #0000FF\">end<\/span>\r\n\r\n<span>&#8942;<\/span><\/pre>\n<p>Now what makes <span style=\"color: #0000FF\">arguments<\/span> better?  Well, in theory the parsing should be easier to understand and read like an English sentence.  But there&#8217;s more &#8211; Check out the Easter Egg at 26s in the video above.  Autocomplete knew the options because they were constrained by <tt>mustBeMember<\/tt>.  Pretty cool, eh?!\n   <\/p>\n<h3>Comments<a name=\"5\"><\/a><\/h3>\n<p>Give it a try and let us know what you think <a href=\"http:\/\/blogs.mathworks.com\/pick\/?p=11042#respond\">here<\/a> or leave a <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/72880#comments\">comment<\/a> for Nathaniel.\n   <\/p>\n<p><script language=\"JavaScript\"><\/p>\n<p><\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\">\n      Published with MATLAB&reg; R2019b<\/p>\n<\/div>\n<p><!--\ndf1ddd6ea3704cf98347948edface2f7 ##### SOURCE BEGIN #####\n%% Leaf Pile\n%\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s pick this week is\n% <http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/72880 |leafpile|> by\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/9069783 Nathaniel Barlow>.\n% \n\n%% \n%\n% As we're _finally_ starting to see cooler temperatures in New England,\n% the leaves are starting to turn.  This fun submission allows MATLAB to\n% make a pile of leaves for you to rake or have your virtual dog play in.\n%\n% To play with this, I'm going to make a simple one line live script with\n% controls to make it so I can play with the settings.  I'll then hide the\n% code so we can just play with the controls and make leaf piles.\n\n\n\n\n%% \n% Now let's do a quick code review of the input parsing step that is used\n% in |leafpile|.  This is the relevant part:\n\nfunction leafpile(N,type)\n%leafpile(leaves,type) makes a random pile of N leaves \n% of type 'oak' or 'maple'.  N. Barlow 9\/28\/19\n%leafpile(N) makes a mixed pile of N leves.\n%leafpile makes a pile of 70 mixed leaves.\n%The mathematical functions describing the leaf shapes were created by Hamid Naderi\n%Yeganeh and given at https:\/\/blogs.scientificamerican.com\/guest-blog\/how-to-draw-with-math\/    \n\nif nargin==0,N=70; type='mixed'; end;\nif nargin==1, type='mixed'; end;\n\nend\n\n%% \n% Using the new\n% <https:\/\/www.mathworks.com\/help\/releases\/R2019b\/matlab\/ref\/arguments.html\n% |arguments|> construct that Jiro\n% <https:\/\/blogs.mathworks.com\/pick\/?p=11026 wrote about> last week (my\n% personal favorite R2019b feature) this could be rewritten with the\n% following which would also validate the inputs:\n\nfunction leafpilea(N,type)\n\narguments\n    N(1,1) double {mustBeInteger, mustBePositive} = 70;\n    type {mustBeMember(type, {'mixed', 'maple', 'oak'})} = 'mixed';\nend\n\nend\n\n%%\n% Now what makes |arguments| better?  Well, in theory the parsing should be\n% easier to understand and read like an English sentence.  But there's more\n% - Check out the Easter Egg at 26s in the video above.  Autocomplete knew\n% the options because they were constrained by |mustBeMember|.  Pretty\n% cool, eh?!\n\n%% Comments\n% \n% Give it a try and let us know what you think\n% <http:\/\/blogs.mathworks.com\/pick\/?p=11042#respond here> or leave a\n% <http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/72880#comments\n% comment> for Nathaniel.\n\n##### SOURCE END ##### df1ddd6ea3704cf98347948edface2f7\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sean&#8216;s pick this week is leafpile by Nathaniel Barlow.<\/p>\n<p>As we&#8217;re finally starting to see cooler temperatures in New England, the leaves are starting to turn.  This fun&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2019\/10\/04\/leaf-pile\/\">read more >><\/a><\/p>\n","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\/11042"}],"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=11042"}],"version-history":[{"count":9,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/11042\/revisions"}],"predecessor-version":[{"id":11062,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/11042\/revisions\/11062"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=11042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=11042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=11042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}