{"id":471,"date":"2010-08-18T17:56:25","date_gmt":"2010-08-18T17:56:25","guid":{"rendered":"https:\/\/blogs.mathworks.com\/videos\/2010\/08\/18\/matlabdo-the-way-of-matlab\/"},"modified":"2010-08-20T18:31:51","modified_gmt":"2010-08-20T18:31:51","slug":"matlabdo-the-way-of-matlab","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/videos\/2010\/08\/18\/matlabdo-the-way-of-matlab\/","title":{"rendered":"MATLABdo: The Way of MATLAB"},"content":{"rendered":"I work in technical support, so very often I get strange requests.  \r\n<p>\r\nReally strange.\r\n<p>\r\nMy first question is &#8220;Why do you want to do that?&#8221;  The reason I ask this is very often new MATLAB users are fighting against the way that MATLAB wants to work.  If <a href=\"http:\/\/en.wikipedia.org\/wiki\/Aikido\">Aikido<\/a> is &#8220;the Way of harmonious spirit&#8221; (or &#8220;the Way of Water&#8221; as I learned it) then MATLABdo should be &#8220;the Way of MATLAB&#8221;.\r\nOnce users understand the MATLAB way of doing things, they can often do a task in a better way and then the specific syntax question they had does not matter.  In fact, their code often becomes simpler, shorter and easier to maintain.\r\n<p>\r\nWhat is The MATLAB Way?\r\n<p>\r\nIt is really hard to say, but I know it when I see someone going against it.  \r\n<p>\r\nLet&#8217;s look at an example of going against MATLAB and how to fix it.\r\n<p>\r\nThis MATLAB user wanted to make a series of variables to store some scalars.  For example:\r\n<p>\r\n<pre><code>\r\na1 = 11;\r\na2 = 22;\r\na3 = 33;\r\n<\/code><\/pre>\r\n<p>\r\nThis lead to the next problem, where they would want to refer to one of these variables, but would not know which until run time.  They just knew they would want to use one with a value of i.\r\n<p>\r\n<code>\r\ni = 3;\r\n<\/code>\r\n<p>\r\nSo, they would want the following when i is three:\r\n<p>\r\n<code>\r\nanswer = a3 + i;\r\n<\/code>\r\n<p>\r\nBut, they would want the following when i is one:\r\n<p>\r\n<code>\r\nanswer = a1 + i;\r\n<\/code>\r\n<p>\r\nThat is when they came to ask me how they could do this.\r\n<p>\r\nThe answer would be:\r\n<p>\r\n<code>\r\nanswer = eval(['a' num2str(i) ' + ' num2str(i) ';']);\r\n<\/code>\r\n<p>\r\nHowever, to answer this question would be a great disservice to this MATLAB user, because their code should look like this:\r\n<p>\r\n<pre><code>\r\na = [11 22 33];\r\ni = 3;\r\nanswer = a(i) + i;\r\n<\/code><\/pre>\r\n<p>\r\nMuch cleaner, it utilizes the fact that MATLAB (MATrix LABratory) thinks of everything as a matrix.\r\n<p>\r\n<hr>\r\n\r\nSince it is hard to describe MATLABdo, can you help me relate experiences of following it, or not following it?","protected":false},"excerpt":{"rendered":"<p>I work in technical support, so very often I get strange requests.  \r\n\r\nReally strange.\r\n\r\nMy first question is &#8220;Why do you want to do that?&#8221;  The reason I ask this is very often new&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/videos\/2010\/08\/18\/matlabdo-the-way-of-matlab\/\">read more >><\/a><\/p>","protected":false},"author":68,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[12,17,24,25],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/471"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/users\/68"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/comments?post=471"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/471\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/media?parent=471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/categories?post=471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/tags?post=471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}