{"id":266,"date":"2011-02-24T13:05:01","date_gmt":"2011-02-24T13:05:01","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2011\/02\/24\/using-m-lint-to-help-write-deployable-code\/"},"modified":"2011-03-03T12:17:27","modified_gmt":"2011-03-03T12:17:27","slug":"using-m-lint-to-help-write-deployable-code","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2011\/02\/24\/using-m-lint-to-help-write-deployable-code\/","title":{"rendered":"Using the MATLAB Code Analyzer to Help Write Deployable Code"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>This week, guest blogger Jacob writes about how to use MATLAB-based tools to develop applications that are deployment-ready\r\n         from inception.\r\n      <\/p>\r\n      <p>Deployed applications rely on the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/compiler\/f12-999353.html\">MATLAB Compiler Runtime<\/a> (the MCR) for execution. The MCR differs from MATLAB in subtle but important ways. You will find it much easier to deploy\r\n         your MATLAB-based applications if <a href=\"https:\/\/blogs.mathworks.com\/loren\/2008\/06\/19\/writing-deployable-code\">you code<\/a> with these differences in mind. You can learn how to develop deployable applications by reading <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/compiler\/br2cqa0-2.html\">our documentation<\/a> and <a href=\"https:\/\/blogs.mathworks.com\/loren\/category\/deployment\/\">blog articles<\/a>. To help you keep track of all the details, we're working on pushing some of this information into our development tools.\r\n         One of first tools we enhanced for deployment is the MATLAB code analyzer, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/mlint.html\">mlint<\/a>.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Turning Deployment Messages On<\/a><\/li>\r\n         <li><a href=\"#2\">Catching Non-Deployable Code<\/a><\/li>\r\n         <li><a href=\"#3\">Limitations of the Code Analyzer<\/a><\/li>\r\n         <li><a href=\"#4\">Use the Code Analyzer but Don't Depend on It<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Turning Deployment Messages On<a name=\"1\"><\/a><\/h3>\r\n   <p>The code analyzer examines MATLAB code to detect possible problems or improvements. The <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/matlab_env\/brqxeeu-151.html\">code analyzer is integrated<\/a> into the MATLAB Editor so you can see its suggestions as you develop your code.\r\n   <\/p>\r\n   <p>Code analyzer suggestions are divided into several categories (you can fine-tune which messages are shown through the Code\r\n      Analyzer Preferences).\r\n   <\/p>\r\n   <p><i>MATLAB Compiler (Deployment) Messages<\/i> alert you when you use certain functions that may not deploy.  However, you might not have noticed these messages before\r\n      since this category is disabled by default. In order to enable this category of messages:\r\n   <\/p>\r\n   <li>Invoke Code Analyzer Preferences by selecting File &gt; Preferences &gt; Code Analyzer<\/li>\r\n   <li>In the Default Settings box, locate the MATLAB Compiler (Deployment) Messages category and click Enable Category.<\/li>\r\n   <h3>Catching Non-Deployable Code<a name=\"2\"><\/a><\/h3>\r\n   <p>With Deployment messages enabled, the code analyzer can help detect when you are developing problematic code.  For example,\r\n      <a href=\"https:\/\/blogs.mathworks.com\/loren\/2008\/06\/19\/writing-deployable-code\">Writing Deployable Code<\/a> used the following as an example of code that may compile but yield undesired results at run-time:\r\n   <\/p><pre>  cd mathFcns\r\n  z = add(x,y);\r\n  cd ..\/stringFcns\r\n  s = add(s1,s2);<\/pre><p>In the MATLAB editor, the code analyzer highlights both instances of the <tt>cd<\/tt> command.  By hovering your cursor over the underlined word, you view the warning message:\r\n   <\/p><pre>MCC use of the CD function is problematic.<\/pre><p>Furthermore, if you click on the message, you get an explanation and suggested action.  In this case, the suggested action\r\n      is to avoid use of the <tt>CD<\/tt> command.\r\n   <\/p>\r\n   <h3>Limitations of the Code Analyzer<a name=\"3\"><\/a><\/h3>\r\n   <p>The code analyzer can help catch many instances of non-deployable code.  Most of these instances involve the use of <a href=\"https:\/\/blogs.mathworks.com\/loren\/2008\/08\/11\/path-management-in-deployed-applications\/\">known problematic functions<\/a> like <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/cd.html\"><tt>cd<\/tt><\/a>, <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/addpath.html\"><tt>addpath<\/tt><\/a> or <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/help.html\"><tt>help<\/tt><\/a>.\r\n   <\/p>\r\n   <p>In fact, the Default Settings window in Code Analyzer Preferences displays all possible messages the code analyzer might display.\r\n      Reading these messages helps you understand what code constructs the code analyzer is likely to catch.\r\n   <\/p>\r\n   <p>While the code analyzer is a useful tool for detecting some instances of non-deployable code, it cannot detect all instances.\r\n      For example, consider the following example which results in no warning or error message:\r\n   <\/p><pre>  set(gca, 'ButtonDownFcn', 'LogButtonPress');<\/pre><p>This code may result in a failure when deployed because MATLAB Compiler's dependency analysis (<a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/depfun.html\"><tt>depfun<\/tt><\/a>) cannot detect that <tt>LogButtonPress<\/tt> is a function that must be included in the application. For this code to work in deployed mode, you must explicitly include\r\n      <tt>LogButtonPress<\/tt> at compile-time (by using the <tt>-a<\/tt> switch in <tt>mcc<\/tt>). In this case, the code analyzer suffers from some of the same limitations that MATLAB Compiler's dependency analysis does---the\r\n      inability to understand the contents of a string.\r\n   <\/p>\r\n   <h3>Use the Code Analyzer but Don't Depend on It<a name=\"4\"><\/a><\/h3>\r\n   <p>The code analyzer can help you to write deployable code, but it is not a substitute for understanding the compilation process\r\n      or how running your application in MATLAB differs from running against the <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/compiler\/f12-999353.html\">MCR<\/a>. This understanding ultimately provides the best foundation for writing deployable code.\r\n   <\/p>\r\n   <p>However, even if you cannot completely depend on the code analyzer to guarantee deployable code, you can still use it to quickly\r\n      catch instances that might otherwise require lengthy debugging to correct.  After all, problems caught while in development\r\n      usually cost much less to fix than problems caught when the application is deployed.\r\n   <\/p>\r\n   <p>Are there other MATLAB coding patterns for which you think the code analyzer should issue deployment warnings? Let us know\r\n      <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=266\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_d34262cc70564b569a3c82ecda078c14() {\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='d34262cc70564b569a3c82ecda078c14 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' d34262cc70564b569a3c82ecda078c14';\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 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_d34262cc70564b569a3c82ecda078c14()\"><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.11<br><\/p>\r\n<\/div>\r\n<!--\r\nd34262cc70564b569a3c82ecda078c14 ##### SOURCE BEGIN #####\r\n%% Using the MATLAB Code Analyzer to Help Write Deployable Code\r\n% This week, guest blogger Jacob writes about how to use MATLAB-based tools\r\n% to develop applications that are deployment-ready from inception.\r\n%\r\n% Deployed applications rely on the \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/compiler\/f12-999353.html\r\n% MATLAB Compiler Runtime> (the MCR) for execution. The MCR differs from\r\n% MATLAB in subtle but important ways. You will find it much easier to \r\n% deploy your MATLAB-based applications if \r\n% <https:\/\/blogs.mathworks.com\/loren\/2008\/06\/19\/writing-deployable-code \r\n% you code> with these differences in mind. You can learn how to develop \r\n% deployable applications by reading \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/compiler\/br2cqa0-2.html our\r\n% documentation> and <https:\/\/blogs.mathworks.com\/loren\/category\/deployment\/\r\n% blog articles>. To help you keep track of all the details,  \r\n% we're working on pushing some of this information into our development\r\n% tools. One of first tools we enhanced for deployment is the MATLAB\r\n% code analyzer, \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/mlint.html mlint>.\r\n\r\n%% Turning Deployment Messages On\r\n% The code analyzer examines MATLAB code to detect possible problems or improvements. \r\n% The <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/matlab_env\/brqxeeu-151.html \r\n% code analyzer is integrated> into the MATLAB Editor so you can see its \r\n% suggestions as you develop your code. \r\n%\r\n% Code analyzer suggestions are divided into several categories (you can \r\n% fine-tune which messages are shown through the Code Analyzer Preferences).\r\n%\r\n% _MATLAB Compiler (Deployment) Messages_ alert you when you use certain \r\n% functions that may not deploy.  However, you might not have noticed \r\n% these messages before since this category is disabled by default.  \r\n% In order to enable this category of messages: \r\n% \r\n% # Invoke Code Analyzer Preferences by selecting File > Preferences > Code\r\n% Analyzer \r\n% # In the Default Settings box, locate the MATLAB Compiler\r\n% (Deployment) Messages category and click Enable Category.\r\n%\r\n%% Catching Non-Deployable Code\r\n% With Deployment messages enabled, the code analyzer can help detect when you are \r\n% developing problematic code.  For example, \r\n% <https:\/\/blogs.mathworks.com\/loren\/2008\/06\/19\/writing-deployable-code \r\n% Writing Deployable Code>\r\n% used the following as an example of code that may compile but yield \r\n% undesired results at run-time:\r\n%    \r\n%    cd mathFcns\r\n%    z = add(x,y);\r\n%    cd ..\/stringFcns\r\n%    s = add(s1,s2);\r\n%    \r\n% In the MATLAB editor, the code analyzer highlights both instances of the |cd|\r\n% command.  By hovering your cursor over the underlined word, you view \r\n% the warning message: \r\n%\r\n%  MCC use of the CD function is problematic.  \r\n%\r\n% Furthermore, if you click on the message, you get an explanation and \r\n% suggested action.  In this case, the suggested action is to avoid use \r\n% of the |CD| command.  \r\n%% Limitations of the Code Analyzer \r\n% The code analyzer can help catch many instances of non-deployable code.  Most of \r\n% these instances involve the use of \r\n% <https:\/\/blogs.mathworks.com\/loren\/2008\/08\/11\/path-management-in-deployed-applications\/ \r\n% known problematic functions> like \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/cd.html |cd|>, \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/addpath.html\r\n% |addpath|> or \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/help.html \r\n% |help|>. \r\n%\r\n% In fact, the Default Settings window in Code Analyzer Preferences displays all \r\n% possible messages the code analyzer might display. Reading these messages helps you \r\n% understand what code constructs the code analyzer is likely to catch. \r\n%\r\n% While the code analyzer is a useful tool for detecting some instances of \r\n% non-deployable code, it cannot detect all instances. For example, \r\n% consider the following example which results in no warning or error message:\r\n%    \r\n%    set(gca, 'ButtonDownFcn', 'LogButtonPress');\r\n%    \r\n% This code may result in a failure when deployed because MATLAB \r\n% Compiler's dependency analysis \r\n% (<https:\/\/www.mathworks.com\/help\/releases\/R2010b\/techdoc\/ref\/depfun.html \r\n% |depfun|>) cannot detect that \r\n% |LogButtonPress| is a function that must be included in the application.  \r\n% For this code to work in deployed mode, you must explicitly include \r\n% |LogButtonPress| at compile-time (by using the |-a| switch in |mcc|).  \r\n% In this case, the code analyzer suffers from some of the same limitations that \r\n% MATLAB Compiler's dependency analysis doesREPLACE_WITH_DASH_DASH-the inability to understand \r\n% the contents of a string.\r\n%% Use the Code Analyzer but Don't Depend on It\r\n% The code analyzer can help you to write deployable code, but it is not a \r\n% substitute for understanding the compilation process or how running \r\n% your application in MATLAB differs from running against the \r\n% <https:\/\/www.mathworks.com\/help\/releases\/R2010b\/toolbox\/compiler\/f12-999353.html \r\n% MCR>.  \r\n% This understanding ultimately provides the best foundation for writing \r\n% deployable code. \r\n%\r\n% However, even if you cannot completely depend on the code analyzer to guarantee \r\n% deployable code, you can still use it to quickly catch instances that \r\n% might otherwise require lengthy debugging to correct.  After all, \r\n% problems caught while in development usually cost much less to fix than \r\n% problems caught when the application is deployed.\r\n%\r\n% Are there other MATLAB coding patterns for which you think the code\r\n% analyzer should issue deployment warnings? Let us know\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=266 here>.\r\n\r\n\r\n\r\n##### SOURCE END ##### d34262cc70564b569a3c82ecda078c14\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      This week, guest blogger Jacob writes about how to use MATLAB-based tools to develop applications that are deployment-ready\r\n         from inception.\r\n      \r\n      Deployed... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2011\/02\/24\/using-m-lint-to-help-write-deployable-code\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[24,13],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/266"}],"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=266"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/266\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}