{"id":17,"date":"2006-01-25T14:23:44","date_gmt":"2006-01-25T19:23:44","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=17"},"modified":"2018-01-08T16:15:50","modified_gmt":"2018-01-08T21:15:50","slug":"whats-the-big-deal","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2006\/01\/25\/whats-the-big-deal\/","title":{"rendered":"What&#8217;s the Big deal?"},"content":{"rendered":"<p>\r\nSo, what <em>is<\/em> the big deal?  To start with, this week marks my 19th anniversary at MathWorks.  Never would I have guessed back then what I am up to now, including <em>talking<\/em> about MATLAB with so many customers.  I look forward to many more years working with MATLAB at MathWorks and meeting and working with so many wonderful customers.\r\n<\/p>\r\n<p>In a recent <a>post on the MATLAB newsgroup<\/a>, Duane Hanselman mentioned that he found something interesting in the help for <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/deal.html\"><i>deal<\/i><\/a>, namely the note:\r\n<blockquote><em>\r\nIn many instances, you can access the data in cell arrays and structure fields without using the <i>deal<\/i> function.<\/em>\r\n<\/blockquote>\r\nand he went on to ask when this was introduced.  To find the answer, I did some archaeology in the MATLAB Release Notes and found some information in the notes for Release 14, MATLAB 7.\r\n<\/p>\r\n<p>\r\nWhy did we introduce this?  Here are some of our thoughts from our design discussions:\r\n<\/p>\r\n<ul>\r\n<li>Users either have to use <i>deal<\/i> or a loop to extract information from a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/cell.html\"><i>cell<\/i> array<\/a> or a field of a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/struct.html\"><i>struct<\/i> array<\/a>.\r\n<\/li>\r\n<li><i>c{:}<\/i> and <i>a(:).fn<\/i> both produce comma-separated lists in MATLAB.  Before this change, you could enclose these expressions in square brackets (<i>[ ]<\/i>), curly braces (<i>{ }<\/i>), or parentheses for a function call (<i>foo( )<\/i>).  Otherwise, they produced as many outputs as the length of the array input, each successively assigned to <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/ans.html\"><i>ans<\/i><\/a>.\r\n<\/li>\r\n<li>Because we were now allowing assignment with a comma separated list on the right hand side where it was not possible before, we were not going to break working code.\r\n<\/li>\r\n<li>\r\nThe idea seemed both natural and welcome when we spoke to some M programmers at MathWorks.\r\n<\/li>\r\n<\/ul>\r\n<\/p>\r\n<p>The biggest problem I see with this new construct is that it seems to have taken a while to get the exposure it deserves.  While there is still clearly a role for <i>deal<\/i>, it's required less often than before MATLAB 7.\r\n<\/p>\r\n<p>\r\nOne code pattern I have seen <i>deal<\/i> used for is renaming variables.  For example, you can swap the values of <i>A<\/i> and <i>B<\/i> with this:\r\n<\/p>\r\n<pre class=\"code\">\r\n    [B,A] = deal(A,B);\r\n<\/pre>\r\n<p>With the use of <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/function-handles.html\">anonymous functions<\/a> you can also accomplish the same outcome:\r\n<\/p>\r\n<pre>\r\n   swap=@(varargin)varargin{nargin:-1:1};\r\n<\/pre>\r\n<p>Now let's see what that does:<\/p>\r\n<pre class=\"code\">\r\n   a = 1;\r\n   b = 2;\r\n   c = 17;\r\n   [a,b,c] = swap(a,b,c)\r\n<\/pre>\r\n<p>and the output looks like this:<\/p>\r\n<pre>\r\n<em>\r\na =\r\n\r\n    17\r\n\r\n\r\nb =\r\n\r\n     2\r\n\r\n\r\nc =\r\n\r\n     1\r\n<\/em><\/pre>\r\n<p>There will be more in future articles on function handles, anonymous functions and nested functions.  I have received suggestions for article topics from several of you and would love to gather even more input.  Please keep the feedback coming!<\/p>","protected":false},"excerpt":{"rendered":"<p>\r\nSo, what is the big deal?  To start with, this week marks my 19th anniversary at MathWorks.  Never would I have guessed back then what I am up to now, including talking about MATLAB with so many... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2006\/01\/25\/whats-the-big-deal\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,5],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/17"}],"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=17"}],"version-history":[{"count":8,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/17\/revisions"}],"predecessor-version":[{"id":2616,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/17\/revisions\/2616"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=17"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=17"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=17"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}