{"id":198,"date":"2009-09-11T14:13:25","date_gmt":"2009-09-11T14:13:25","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2009\/09\/11\/matlab-release-2009b-best-new-feature-or\/"},"modified":"2009-09-15T13:42:50","modified_gmt":"2009-09-15T13:42:50","slug":"matlab-release-2009b-best-new-feature-or","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2009\/09\/11\/matlab-release-2009b-best-new-feature-or\/","title":{"rendered":"MATLAB Release 2009b &#8211; Best New Feature or ~?"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/products\/new_products\/latest_features.html\">MATLAB R2009b<\/a> was recently released.  My favorite new language feature is the introduction of the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/matlab_prog\/bresuxt-1.html#br67dkp-1\"><tt>~<\/tt> notation<\/a> to denote missing inputs in function declarations, and missing outputs in calls to functions.  Let me show you how this works.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Unused Outputs<\/a><\/li>\r\n         <li><a href=\"#4\">Unused Inputs<\/a><\/li>\r\n         <li><a href=\"#6\">Can M-Lint Help?<\/a><\/li>\r\n         <li><a href=\"#11\">What's Your Favorite New Feature?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Unused Outputs<a name=\"1\"><\/a><\/h3>\r\n   <p>I have occasionally found that I would like the indices from sorting a vector, but I don't need the sorted values.  In the\r\n      past, I wrote one of these code variants :\r\n   <\/p><pre>          [dummy, ind] = sort(X)\r\n          [ind, ind] = sort(X)<\/pre><p>In the first case, I end up with a variable <tt>dummy<\/tt> in my workspace that I don't need. If my data to sort, <tt>X<\/tt>, has a large number of elements, I will have an unneeded large array hanging around afterwards.  In the second case, I am\r\n      banking on MATLAB assigning outputs in order, left to right, and I create somewhat less legible code, but I don't have an\r\n      extra array hanging around afterwards.\r\n   <\/p>\r\n   <p>Now you can write this instead:<\/p><pre>          [~, ind] = sort(X)<\/pre><p>and I hope you find your code readable, with the clear intention to not use the first output variable.<\/p>\r\n   <h3>Unused Inputs<a name=\"4\"><\/a><\/h3>\r\n   <p>You can similarly designate unused inputs with <tt>~<\/tt> in function declarations.  Here's how you'd define the interface where the second input is ignored.\r\n   <\/p><pre>          function out = mySpecialFunction(X,~,dim)<\/pre><p>You might ask why that is useful.  If I don't use the second input, why put it in at all?  The answer is that your function\r\n      might be called by some other function that expects to send three inputs.  This happens for many GUI callbacks, and particularly\r\n      those you generate using <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/guide.html\"><tt>guide<\/tt><\/a>. So your function needs to take three inputs. But if it is never going to use the second input, you can denote the second\r\n      one with <tt>~<\/tt>.\r\n   <\/p>\r\n   <h3>Can M-Lint Help?<a name=\"6\"><\/a><\/h3>\r\n   <p>Yes!  Consider this function <tt>mySpecialFunction<\/tt> shown here.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">type <span style=\"color: #A020F0\">mySpecialFunction<\/span><\/pre><pre style=\"font-style:oblique\">\r\nfunction ind = mySpecialFunction(X,second,dim)\r\n% mySpecialFunction Function to illustrate ~ for inputs and outputs.\r\n\r\n[dummy,ind] = sort(X,dim);\r\n<\/pre><p>Running <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/mlint.html\"><tt>mlint<\/tt><\/a> on this code produces two messages.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">msgs = mlint(<span style=\"color: #A020F0\">'mySpecialFunction'<\/span>);\r\ndisp(msgs(1).message(1:50))\r\ndisp(msgs(1).message(51:end))\r\ndisp(<span style=\"color: #A020F0\">' '<\/span>)\r\ndisp(msgs(2).message(1:49))\r\ndisp(msgs(2).message(50:end))<\/pre><pre style=\"font-style:oblique\">Input argument 'second' might be unused, although \r\na later one is used.  Consider replacing it by ~.\r\n \r\nThe value assigned here to 'dummy' appears to be \r\nunused.  Consider replacing it by ~.\r\n<\/pre><p>Since M-Lint is running continuously in the editor, you would see these messages as you edit the file.  Here's a cleaned up\r\n      version of the file.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">type <span style=\"color: #A020F0\">mySpecialFunction1<\/span><\/pre><pre style=\"font-style:oblique\">\r\nfunction ind = mySpecialFunction1(X,~,dim)\r\n% mySpecialFunction Function to illustrate ~ for inputs and outputs.\r\n\r\n[~,ind] = sort(X,dim);\r\n<\/pre><p>And let's see what M-Lint finds.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">mlint <span style=\"color: #A020F0\">mySpecialFunction1<\/span><\/pre><p>It finds nothing at all.<\/p>\r\n   <h3>What's Your Favorite New Feature?<a name=\"11\"><\/a><\/h3>\r\n   <p>Have you looked through the <a href=\"https:\/\/www.mathworks.com\/products\/matlab\/whatsnew.html\">new features<\/a> for R2009b?  What's your favorite?  Let me know <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=198#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_27713d2e85df444bb2fce1ee020e0067() {\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='27713d2e85df444bb2fce1ee020e0067 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 27713d2e85df444bb2fce1ee020e0067';\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 2009 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_27713d2e85df444bb2fce1ee020e0067()\"><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.9<br><\/p>\r\n<\/div>\r\n<!--\r\n27713d2e85df444bb2fce1ee020e0067 ##### SOURCE BEGIN #####\r\n%% MATLAB Release 2009b - Best New Feature or ~?\r\n% <https:\/\/www.mathworks.com\/products\/new_products\/latest_features.html MATLAB R2009b>\r\n% was recently released.  My favorite new language feature is\r\n% the introduction of the \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/matlab_prog\/bresuxt-1.html#br67dkp-1 |~| notation>\r\n% to denote missing inputs in function declarations, and missing outputs in\r\n% calls to functions.  Let me show you how this works.\r\n%% Unused Outputs\r\n% I have occasionally found that I would like the indices from sorting a\r\n% vector, but I don't need the sorted values.  In the past, I wrote one of\r\n% these code variants :\r\n%\r\n%            [dummy, ind] = sort(X)\r\n%            [ind, ind] = sort(X)\r\n%%\r\n% In the first case, I end up with a variable |dummy| in my workspace that\r\n% I don't need. If my data to sort, |X|, has a large number of elements, I\r\n% will have an unneeded large array hanging around afterwards.  In the\r\n% second case, I am banking on MATLAB assigning outputs in order, left to\r\n% right, and I create somewhat less legible code, but I don't have an extra\r\n% array hanging around afterwards.  \r\n%%\r\n% Now you can write this instead:\r\n%\r\n%            [~, ind] = sort(X)\r\n%\r\n% and I hope you find your code readable, with the clear intention to not\r\n% use the first output variable.\r\n%% Unused Inputs\r\n% You can similarly designate unused inputs with |~| in function\r\n% declarations.  Here's how you'd define the interface where the second\r\n% input is ignored.\r\n%\r\n%            function out = mySpecialFunction(X,~,dim)\r\n%\r\n%%\r\n% You might ask why that is useful.  If I don't use the second input, why\r\n% put it in at all?  The answer is that your function might be called by\r\n% some other function that expects to send three inputs.  This happens for\r\n% many GUI callbacks, and particularly those you generate using \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/guide.html |guide|>.\r\n% So your function needs to take three inputs. But if it is never going to\r\n% use the second input, you can denote the second one with |~|.\r\n%% Can M-Lint Help?\r\n% Yes!  Consider this function |mySpecialFunction| shown here.\r\ntype mySpecialFunction\r\n%%\r\n% Running <https:\/\/www.mathworks.com\/help\/releases\/R2009b\/techdoc\/ref\/mlint.html |mlint|>\r\n% on this code produces two messages.\r\nmsgs = mlint('mySpecialFunction');\r\ndisp(msgs(1).message(1:50))\r\ndisp(msgs(1).message(51:end))\r\ndisp(' ')\r\ndisp(msgs(2).message(1:49))\r\ndisp(msgs(2).message(50:end))\r\n%%\r\n% Since M-Lint is running continuously in the editor, you would see these\r\n% messages as you edit the file.  Here's a cleaned up version of the file.\r\ntype mySpecialFunction1\r\n%%\r\n% And let's see what M-Lint finds.\r\nmlint mySpecialFunction1\r\n%%\r\n% It finds nothing at all.\r\n%% What's Your Favorite New Feature?\r\n% Have you looked through the\r\n% <https:\/\/www.mathworks.com\/products\/matlab\/whatsnew.html new features> for\r\n% R2009b?  What's your favorite?  Let me know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=198#respond here>.\r\n\r\n##### SOURCE END ##### 27713d2e85df444bb2fce1ee020e0067\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      MATLAB R2009b was recently released.  My favorite new language feature is the introduction of the ~ notation to denote missing inputs in function declarations, and missing outputs in... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/09\/11\/matlab-release-2009b-best-new-feature-or\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/198"}],"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=198"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/198\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=198"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=198"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=198"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}