{"id":295,"date":"2011-10-28T17:22:49","date_gmt":"2011-10-28T17:22:49","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2011\/10\/28\/happy-halloween\/"},"modified":"2011-10-25T17:28:51","modified_gmt":"2011-10-25T17:28:51","slug":"happy-halloween","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2011\/10\/28\/happy-halloween\/","title":{"rendered":"Happy Halloween!"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Today I&#8217;d like to introduce my guest bloggers <a href=\"mailto:sarah.zaranek@mathworks.com\">Sarah Wait Zaranek<\/a> and <a href=\"mailto:jiro.doke@mathworks.com\">Jiro Doke<\/a>. Sarah previously has <a href=\"https:\/\/blogs.mathworks.com\/loren\/2008\/06\/25\/speeding-up-matlab-applications\/\">written<\/a> about speeding up code from a customer to get acceptable performance and Jiro has previously written about making presentation\r\n         quality graphics and is a co-author of <a href=\"https:\/\/blogs.mathworks.com\/pick\/\">File Exchange Pick of the Week<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Halloween<\/a><\/li>\r\n         <li><a href=\"#2\">Let's see what it does<\/a><\/li>\r\n         <li><a href=\"#7\">Dissecting the function<\/a><\/li>\r\n         <li><a href=\"#10\">The files<\/a><\/li>\r\n         <li><a href=\"#11\">Fun with RAND<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Halloween<a name=\"1\"><\/a><\/h3>\r\n   <p>In some parts of the world, people celebrate <a href=\"http:\/\/en.wikipedia.org\/wiki\/Halloween\">Halloween<\/a>, which occurs on October 31. In the spirit (pun intended) of the holiday, we created a Halloween-inspired function.\r\n   <\/p>\r\n   <p>If you have ever used <tt>why<\/tt> in MATLAB, you will definitely recognize the other inspiration for our function.\r\n   <\/p>\r\n   <h3>Let's see what it does<a name=\"2\"><\/a><\/h3>\r\n   <p>I wonder what Loren is going to be for Halloween!<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">halloween <span style=\"color: #A020F0\">Loren<\/span><\/pre><pre style=\"font-style:oblique\">Loren, you are scary enough without a costume!<\/pre><p>How about not just any Loren, but <i>our<\/i> Loren?\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">halloween <span style=\"color: #A020F0\">Loren<\/span> <span style=\"color: #A020F0\">Shure<\/span><\/pre><pre style=\"font-style:oblique\">Loren Shure, for Halloween, you will be a screaming Gremlin.\r\n<\/pre><p>Oh my!  What about Sarah and Jiro?<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">halloween <span style=\"color: #A020F0\">Sarah<\/span>\r\nhalloween <span style=\"color: #A020F0\">Jiro<\/span><\/pre><pre style=\"font-style:oblique\">Sarah, for Halloween, you will be a sleepy Bogeyman swimming in blood.\r\nJiro, for Halloween, you will be a Dracula baring teeth to the ends of the earth.\r\n<\/pre><p>Note that the function works with both Command and Function syntax, which is explained <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/matlab_prog\/f7-58289.html\">here<\/a>.\r\n   <\/p>\r\n   <p>What happens if we don't put a name in?<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">halloween<\/pre><pre style=\"font-style:oblique\">Nameless, you are scary enough without a costume!<\/pre><p>It still works. Looks like Nameless is going to have a fun holiday.<\/p>\r\n   <h3>Dissecting the function<a name=\"7\"><\/a><\/h3>\r\n   <p>The main function does several things<\/p>\r\n   <div>\r\n      <ul>\r\n         <li>loads Halloween data from a MAT-file<\/li>\r\n         <li>generates a Halloween name based on the name entered and the current time<\/li>\r\n         <li>prints the Halloween name to the screen<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>It also includes an \"easter egg\".<\/p>\r\n   <p>Let's look inside <tt>halloween<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">type <span style=\"color: #A020F0\">halloween<\/span><\/pre><pre style=\"font-style:oblique\">\r\nfunction halloween(varargin)\r\n% HALLOWEEN   Print out your Halloween name.\r\n%     HALLOWEEN NAME prints a randomly generated Halloween name based on\r\n%     NAME (optional). NAME must be a character array or multiple character\r\n%     arrays.\r\n%\r\n% Example:\r\n%     halloween('Loren')\r\n%     halloween('Sarah Zaranek')\r\n%     halloween Jiro Doke\r\n\r\n% Copyright 2011 The MathWorks, Inc.\r\n\r\n% Default name\r\nif nargin == 0\r\n    name = 'Nameless';\r\nelse\r\n    % Make sure all inputs are character arrays\r\n    cellfun(@(x,y) validateattributes(x, {'char'}, {'row'}, y), ...\r\n        varargin, num2cell(1:nargin));\r\n    % Construct a single name from multiple strings\r\n    name = deblank(sprintf('%s ', varargin{:}));\r\nend\r\n\r\n% Special case\r\n[~, ~, ~, ~, ~, sec] = datevec(now);\r\nrng(sec*1000);\r\n\r\nif randi(100) &gt; 95  % 5 percent chance of getting this special message.\r\n    fprintf('%s, you are scary enough without a costume!', name);\r\n    return;\r\nend\r\n\r\n% Set seed based on name and time.\r\nseed = sum(name) + floor(now);\r\nrng(seed);\r\n\r\n% Load Halloween data\r\ndata = load('HalloweenData');\r\n\r\n% Generate Halloween name\r\nhalloween_name = getName(data);\r\n\r\n% Print out Halloween name\r\nprintName(name, halloween_name);\r\n<\/pre><p>We will dig into <tt>getName<\/tt> and <tt>printName<\/tt> shortly, but let's just see what <tt>halloween<\/tt> does.\r\n   <\/p>\r\n   <p><b>Default Name<\/b><\/p>\r\n   <p>First, we check to see if the user supplied a name. This uses <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/nargin.html\"><tt>nargin<\/tt><\/a> which is highly useful when writing your own custom functions. <tt>nargin<\/tt> gives the number of arguments the user inputs when calling the function. If it is zero, we assign a default name of \"Nameless\".\r\n      This is an easy way to set default behavior for your function - allowing users to either run it with or without input arguments.\r\n      Take a look at this previous <a href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/05\/05\/nice-way-to-set-function-defaults\/\">post<\/a> by Loren on this topic.\r\n   <\/p>\r\n   <p>If an input (or inputs) is provided, we do a check to see if they are valid by using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/validateattributes.html\"><tt>validateattributes<\/tt><\/a>. Also, to provide a nice way of calling using Command syntax, we allow multiple input arguments to construct a longer string\r\n      (see the example above with \"Loren Shure\").\r\n   <\/p><pre>   if nargin == 0\r\n       name = 'Nameless';\r\n   else\r\n       % Make sure all inputs are character arrays\r\n       cellfun(@(x,y) validateattributes(x, {'char'}, {'row'}, y), ...\r\n           varargin, num2cell(1:nargin));\r\n       % Construct a single name from multiple strings\r\n       name = deblank(sprintf('%s ', varargin{:}));\r\n   end<\/pre><p><b>Easter Egg<\/b><\/p>\r\n   <p>Then, we generate a random number based on the current time (using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/now.html\"><tt>now<\/tt><\/a>) to let us print a special message (Easter Egg) roughly 5% of the time. We use the new-ish <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/rng.html\"><tt>rng<\/tt><\/a> function which allows us to easily set a single seed for <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/rand.html\"><tt>rand<\/tt><\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/randi.html\"><tt>randi<\/tt><\/a>, and <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/randn.html\"><tt>randn<\/tt><\/a>. The seed is generated using the millisecond output from <tt>now<\/tt>.\r\n   <\/p><pre>   [~, ~, ~, ~, ~, sec] = datevec(now);\r\n   rng(sec*1000);\r\n   if randi(100) &gt; 95\r\n       fprintf('%s, you are scary enough without a costume!', name);\r\n       return;\r\n   end<\/pre><p><b>Set Random Seed<\/b><\/p>\r\n   <p>Then if the special case isn't reached, we generate a new seed for our random number generator. This time, it is based off\r\n      of the input name and the time (again given by <tt>now<\/tt>, but this time we just take the day information). We add up the letters of the name to create a single number, using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/sum.html\"><tt>sum<\/tt><\/a>. Note that if the array is a character string, the result is automatically converted to a double.\r\n   <\/p><pre>   seed = sum(name) + floor(now);\r\n   rng(seed);<\/pre><p><b>Create Halloween Name<\/b><\/p>\r\n   <p>We are using the work by <a href=\"http:\/\/www.seventhsanctum.com\/\">Steven Savage<\/a> as inspiration. The Halloween name can have 4 different parts:\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>a creature (like \"ghost\")<\/li>\r\n         <li>a descriptor (like \"ghastly\")<\/li>\r\n         <li>an action (like \"floating\")<\/li>\r\n         <li>a phrase (like \"above your head\")<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>You are always a creature, and you have one of the following three configurations:<\/p>\r\n   <div>\r\n      <ul>\r\n         <li>a creature + a descriptor<\/li>\r\n         <li>a creature + an action<\/li>\r\n         <li>a creature + a descriptor and an action<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>We use <tt>randi<\/tt> to randomly choose between the three configurations. Since the output of the function is 1, 2, or 3, this is a perfect case\r\n      for <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/switch.html\"><tt>switch<\/tt> - <tt>case<\/tt><\/a> construct. Within each <tt>case<\/tt> block, we call <tt>randi<\/tt> again (we love that function!) to randomly select the specific entry from the loaded list of descriptors and actions. In\r\n      the \"Action\" block, we call <tt>randi<\/tt> once again to add a prepositional phrase 50% of the time.\r\n   <\/p><pre>   configID = randi(3);\r\n   switch configID\r\n       case 1 % Descriptor\r\n           &lt;choose a descriptor&gt;\r\n       case 2 % Action\r\n           &lt;choose an action&gt;\r\n           if randi(2) == 1 % Additional phrase\r\n              &lt;choose a phrase&gt;\r\n           end\r\n       case 3 % Descriptor + Action\r\n           &lt;choose a descriptor and an action&gt;\r\n   end<\/pre><p>Let's see the whole function. Note that we sorted the Halloween name parts at the beginning, because Sarah likes things in\r\n      alphabetical order. The creature names were inspired by this <a title=\"http:\/\/www.mythicalcreaturesguide.com\/page\/List+of+Mythical+Creatures (link no longer works)\">web site<\/a>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">type <span style=\"color: #A020F0\">getName<\/span><\/pre><pre style=\"font-style:oblique\">\r\nfunction halloween_name = getName(data)\r\n% Copyright 2011 The MathWorks, Inc.\r\n\r\n% Sort data alphabetically\r\ndata.Creatures = sort(data.Creatures);\r\ndata.Descriptors = sort(data.Descriptors);\r\ndata.Actions = sort(data.Actions);\r\ndata.Phrases = sort(data.Phrases);\r\n\r\n% Choose a random creature\r\ncreatureID = randi(length(data.Creatures));\r\n\r\n% Choose one of 3: Descriptor, Action, Descriptor+Action\r\nconfigID = randi(3);\r\n\r\nswitch configID\r\n    case 1 % Descriptor\r\n        descriptorID = randi(length(data.Descriptors));\r\n        halloween_name = [data.Descriptors{descriptorID}, ' ', ...\r\n            data.Creatures{creatureID}];\r\n   \r\n    case 2 % Action\r\n        actionID = randi(length(data.Actions));\r\n        halloween_name = [data.Creatures{creatureID}, ' ', ...\r\n            data.Actions{actionID}];\r\n        \r\n        % Additional phrase\r\n        if randi(2) == 1\r\n            rand_phrase = randi(length(data.Phrases));\r\n            halloween_name = [halloween_name, ' ', ...\r\n                data.Phrases{rand_phrase}];\r\n        end\r\n        \r\n    case 3 % Descriptor + Action\r\n        descriptorID = randi(length(data.Descriptors));\r\n        actionID = randi(length(data.Actions));\r\n        halloween_name = [data.Descriptors{descriptorID}, ' ', ...\r\n            data.Creatures{creatureID}, ' ', data.Actions{actionID}];\r\nend\r\n\r\n<\/pre><p><b>Print Halloween Name<\/b><\/p>\r\n   <p>Finally, we print out your Halloween name to the screen. We do a simple grammar check to use the appropriate article based\r\n      on the first letter of the name.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">type <span style=\"color: #A020F0\">printName<\/span><\/pre><pre style=\"font-style:oblique\">\r\nfunction printName(name, halloween_name)\r\n% Copyright 2011 The MathWorks, Inc.\r\n\r\n% Check the first letter of the name\r\nif ismember(lower(halloween_name(1)), {'a', 'e', 'i', 'o', 'u'})\r\n    fprintf('%s, for Halloween, you will be an %s.\\n', name, halloween_name);\r\nelse\r\n    fprintf('%s, for Halloween, you will be a %s.\\n', name, halloween_name);\r\nend\r\n<\/pre><h3>The files<a name=\"10\"><\/a><\/h3>\r\n   <p>Download all the necessary files <a href=\"https:\/\/blogs.mathworks.com\/images\/loren\/2011_10_Halloween\/HalloweenNameGenerator.zip\">here<\/a> to try it out yourself! You can use your own words by creating a MAT file named \"HalloweenData.mat\" containing four cell\r\n      arrays: Creatures, Descriptors, Actions, and Phrases.\r\n   <\/p>\r\n   <h3>Fun with RAND<a name=\"11\"><\/a><\/h3>\r\n   <p>What kind of interesting things have you done using a random number generator? Tell us about it <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=295#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_9acbcdb7b4ae43f88938a4beabc4227a() {\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='9acbcdb7b4ae43f88938a4beabc4227a ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 9acbcdb7b4ae43f88938a4beabc4227a';\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 = 'Sarah Wait Zaranek and Jiro Doke';\r\n        copyright = 'Copyright 2011 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_9acbcdb7b4ae43f88938a4beabc4227a()\"><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.13<br><\/p>\r\n<\/div>\r\n<!--\r\n9acbcdb7b4ae43f88938a4beabc4227a ##### SOURCE BEGIN #####\r\n%% Happy Halloween!\r\n% Today I\u00e2\u20ac\u2122d like to introduce my guest bloggers\r\n% <mailto:sarah.zaranek@mathworks.com Sarah Wait Zaranek> and\r\n% <mailto:jiro.doke@mathworks.com Jiro Doke>. Sarah previously has\r\n% <https:\/\/blogs.mathworks.com\/loren\/2008\/06\/25\/speeding-up-matlab-applications\/\r\n% written> about speeding up code from a customer to get acceptable\r\n% performance and Jiro has previously written about making presentation\r\n% quality graphics and is a co-author of <https:\/\/blogs.mathworks.com\/pick\/\r\n% File Exchange Pick of the Week>.\r\n\r\n%% Halloween\r\n% In some parts of the world, people celebrate\r\n% <http:\/\/en.wikipedia.org\/wiki\/Halloween Halloween>, which occurs on\r\n% October 31. In the spirit (pun intended) of the holiday, we created a\r\n% Halloween-inspired function.\r\n%\r\n% If you have ever used |why| in MATLAB, you will definitely recognize the\r\n% other inspiration for our function.\r\n\r\n%% Let's see what it does\r\n% I wonder what Loren is going to be for Halloween!\r\n\r\nhalloween Loren\r\n\r\n%%\r\n% How about not just any Loren, but _our_ Loren?\r\n\r\nhalloween Loren Shure\r\n\r\n%%\r\n% Oh my!  What about Sarah and Jiro?\r\n\r\nhalloween Sarah\r\nhalloween Jiro\r\n\r\n%%\r\n% Note that the function works with both Command and Function syntax, which\r\n% is explained\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/matlab_prog\/f7-58289.html here>.\r\n%\r\n% What happens if we don't put a name in?\r\n\r\nhalloween\r\n\r\n%%\r\n% It still works. Looks like Nameless is going to have a fun holiday.\r\n\r\n%% Dissecting the function\r\n% The main function does several things\r\n%\r\n% * loads Halloween data from a MAT-file\r\n% * generates a Halloween name based on the name entered and the current\r\n% time\r\n% * prints the Halloween name to the screen\r\n%\r\n% It also includes an \"easter egg\".\r\n%\r\n% Let's look inside |halloween|.\r\n\r\ntype halloween\r\n\r\n%%\r\n% We will dig into |getName| and |printName| shortly, but let's just see\r\n% what |halloween| does.\r\n%\r\n% *Default Name*\r\n%\r\n% First, we check to see if the user supplied a name. This uses\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/nargin.html |nargin|> which is\r\n% highly useful when writing your own custom functions. |nargin| gives the\r\n% number of arguments the user inputs when calling the function. If it is\r\n% zero, we assign a default name of \"Nameless\". This is an easy way to set\r\n% default behavior for your function - allowing users to either run it with\r\n% or without input arguments. Take a look at this previous\r\n% <https:\/\/blogs.mathworks.com\/loren\/2009\/05\/05\/nice-way-to-set-function-defaults\/\r\n% post> by Loren on this topic.\r\n%\r\n% If an input (or inputs) is provided, we do a check to see if they are\r\n% valid by using\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/validateattributes.html\r\n% |validateattributes|>. Also, to provide a nice way of calling using\r\n% Command syntax, we allow multiple input arguments to construct a longer\r\n% string (see the example above with \"Loren Shure\").\r\n%\r\n%     if nargin == 0\r\n%         name = 'Nameless';\r\n%     else\r\n%         % Make sure all inputs are character arrays\r\n%         cellfun(@(x,y) validateattributes(x, {'char'}, {'row'}, y), ...\r\n%             varargin, num2cell(1:nargin));\r\n%         % Construct a single name from multiple strings\r\n%         name = deblank(sprintf('%s ', varargin{:}));\r\n%     end\r\n%\r\n% *Easter Egg*\r\n%\r\n% Then, we generate a random number based on the current time (using\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/now.html |now|>) to let us\r\n% print a special message (Easter Egg) roughly 5% of the time. We use the new-ish\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/rng.html |rng|> function which\r\n% allows us to easily set a single seed for\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/rand.html |rand|>,\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/randi.html |randi|>, and\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/randn.html |randn|>. The seed\r\n% is generated using the millisecond output from |now|.\r\n%\r\n%     [~, ~, ~, ~, ~, sec] = datevec(now);\r\n%     rng(sec*1000);\r\n%     if randi(100) > 95\r\n%         fprintf('%s, you are scary enough without a costume!', name);\r\n%         return;\r\n%     end\r\n%\r\n% *Set Random Seed*\r\n%\r\n% Then if the special case isn't reached, we generate a new seed for our\r\n% random number generator. This time, it is based off of the input name and\r\n% the time (again given by |now|, but this time we just take the day\r\n% information). We add up the letters of the name to create a single\r\n% number, using <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/sum.html |sum|>.\r\n% Note that if the array is a character string, the result is automatically\r\n% converted to a double.\r\n%\r\n%     seed = sum(name) + floor(now);\r\n%     rng(seed);\r\n%\r\n% *Create Halloween Name*\r\n%\r\n% We are using the work by <http:\/\/www.seventhsanctum.com\/ Steven Savage>\r\n% as inspiration. The Halloween name can have 4 different parts:\r\n%\r\n% * a creature (like \"ghost\")\r\n% * a descriptor (like \"ghastly\")\r\n% * an action (like \"floating\")\r\n% * a phrase (like \"above your head\")\r\n%\r\n% You are always a creature, and you have one of the following three\r\n% configurations:\r\n%\r\n% * a creature + a descriptor\r\n% * a creature + an action\r\n% * a creature + a descriptor and an action\r\n%\r\n% We use |randi| to randomly choose between the three configurations. Since\r\n% the output of the function is 1, 2, or 3, this is a perfect case for\r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2011b\/techdoc\/ref\/switch.html |switch| - |case|>\r\n% construct. Within each |case| block, we call |randi| again (we love that\r\n% function!) to randomly select the specific entry from the loaded list of\r\n% descriptors and actions. In the \"Action\" block, we call |randi| once\r\n% again to add a prepositional phrase 50% of the time.\r\n%\r\n%     configID = randi(3);\r\n%     switch configID\r\n%         case 1 % Descriptor\r\n%             <choose a descriptor>\r\n%         case 2 % Action\r\n%             <choose an action>\r\n%             if randi(2) == 1 % Additional phrase\r\n%                <choose a phrase>\r\n%             end\r\n%         case 3 % Descriptor + Action\r\n%             <choose a descriptor and an action>\r\n%     end\r\n%\r\n% Let's see the whole function. Note that we sorted the Halloween name\r\n% parts at the beginning, because Sarah likes things in alphabetical order.\r\n% The creature names were inspired by this\r\n% <http:\/\/www.mythicalcreaturesguide.com\/page\/List+of+Mythical+Creatures\r\n% web site>.\r\n\r\ntype getName\r\n\r\n%%\r\n% *Print Halloween Name*\r\n%\r\n% Finally, we print out your Halloween name to the screen. We do a simple\r\n% grammar check to use the appropriate article based on the first letter of\r\n% the name.\r\n\r\ntype printName\r\n\r\n%% The files\r\n% Download all the necessary files\r\n% <https:\/\/blogs.mathworks.com\/images\/loren\/2011_10_Halloween\/HalloweenNameGenerator.zip\r\n% here> to try it out yourself! You can use your own words by creating a\r\n% MAT file named \"HalloweenData.mat\" containing four cell arrays:\r\n% Creatures, Descriptors, Actions, and Phrases.\r\n\r\n%% Fun with RAND\r\n% What kind of interesting things have you done using a random number\r\n% generator? Tell us about it\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=295#respond here>.\r\n##### SOURCE END ##### 9acbcdb7b4ae43f88938a4beabc4227a\r\n-->\r\n","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Today I&#8217;d like to introduce my guest bloggers Sarah Wait Zaranek and Jiro Doke. Sarah previously has written about speeding up code from a customer to get acceptable performance... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2011\/10\/28\/happy-halloween\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[33,28],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/295"}],"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=295"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/295\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}