{"id":5890,"date":"2015-03-13T09:00:09","date_gmt":"2015-03-13T13:00:09","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5890"},"modified":"2015-03-12T09:50:43","modified_gmt":"2015-03-12T13:50:43","slug":"deploying-p-code-files","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2015\/03\/13\/deploying-p-code-files\/","title":{"rendered":"Deploying P-code Files"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/26919-deploypcode-source-dir--target-dir--varargin-\"><tt>deploypcode<\/tt><\/a> by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1672378\">Sven<\/a>.\r\n   <\/p>\r\n   <p>Sven's no stranger to Pick of the Week. This time, he has created a tool that allows you to easily deploy your MATLAB files\r\n      as <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/protect-your-source-code.html#bsc58yj-2\">P-files<\/a> (protected files). P-files are obfuscated versions of your MATLAB files and are good for hiding the contents of your MATLAB\r\n      source code. Note, however, that P-files are not <i>encrypted<\/i> and should not be considered secure.\r\n   <\/p>\r\n   <p>Sven's <tt>deploypcode<\/tt> can take the contents of a particular folder and P-code all of the MATLAB files, including files in subfolders. The function\r\n      provides a number of additional features, as well. Two of my favorites are the ability to keep the help text and the ability\r\n      to copy additional files for easy deployment. For example, let's say that I have some MATLAB files and a data file.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">dir <span style=\"color: #A020F0\">analysis<\/span><\/pre><pre style=\"font-style:oblique\">\r\n.                     MainAnalysis.m        importEnergyFile.m    \r\n..                    createCustomFigure.m  \r\nData.csv              createEnergyFit.m     \r\n\r\n<\/pre><p>I will P-code them and copy the additional data file (Data.csv) into a different folder.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">deploypcode(<span style=\"color: #A020F0\">'analysis'<\/span>,<span style=\"color: #A020F0\">'distrib'<\/span>,<span style=\"color: #A020F0\">'copyDirectStrings'<\/span>,<span style=\"color: #A020F0\">'.csv'<\/span>)<\/pre><pre style=\"font-style:oblique\">Analysing \"C:\\MyStuff\\Work\\POTW\\2015_03_13_DeployP\\analysis\":\r\n(4 .Ms, 1 COPYs, 0 sub-dirs)\r\nP-coding FILE:  MainAnalysis.m ... \r\nMainAnalysis.m\t (0.000000)\r\ndone.  424 chars written to help file\r\nP-coding FILE:  createCustomFigure.m ... \r\ncreateCustomFigure.m\t (0.000000)\r\ndone.  125 chars written to help file\r\nP-coding FILE:  createEnergyFit.m ... \r\ncreateEnergyFit.m\t (0.000000)\r\ndone.  324 chars written to help file\r\nP-coding FILE:  importEnergyFile.m ... \r\nimportEnergyFile.m\t (0.000000)\r\ndone.  485 chars written to help file\r\nCopying:  Data.csv ... done.\r\n<\/pre><p>In my 'distrib' folder, I have the P-files, the corresponding MATLAB files (which contain the help text), and my CSV file.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">dir <span style=\"color: #A020F0\">distrib<\/span><\/pre><pre style=\"font-style:oblique\">\r\n.                     MainAnalysis.p        createEnergyFit.p     \r\n..                    createCustomFigure.m  importEnergyFile.m    \r\nData.csv              createCustomFigure.p  importEnergyFile.p    \r\nMainAnalysis.m        createEnergyFit.m     \r\n\r\n<\/pre><p>For those of you who want to learn some good coding practices, take a look at Sven's code. A few things to mention:<\/p>\r\n   <div>\r\n      <ul>\r\n         <li>Thorough help. Just type <tt>help deploypcode<\/tt> and you'll learn everything you need to know about the function.\r\n         <\/li>\r\n         <li>Plenty of error-checking. He guards against potential problems, such as missing source or target folders.<\/li>\r\n         <li>Robust input parsing. He uses <a title=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/inputparser-class.html (link no longer works)\"><tt>inputParser<\/tt><\/a> to deal with various optional arguments.\r\n         <\/li>\r\n         <li>Use of <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/oncleanup.html\"><tt>onCleanup<\/tt><\/a>. To ensure that some clean up tasks are performed, even in the case of errors, he uses the <tt>onCleanup<\/tt> function.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Finally, be sure to check out <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/packaging-guis-as-apps.html\">App Packaging<\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/creating-help.html\">Toolbox Distribution<\/a> to learn about other ways to distribute your programs.\r\n   <\/p>\r\n   <p><b>Comments<\/b><\/p>\r\n   <p>Give this a try and let us know what you think <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5890#respond\">here<\/a> or leave a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/26919-deploypcode-source-dir--target-dir--varargin-#comments\">comment<\/a> for Sven.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_98cd9320599b4ed69c652f2da3603a1e() {\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='98cd9320599b4ed69c652f2da3603a1e ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 98cd9320599b4ed69c652f2da3603a1e';\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 = '';\r\n        copyright = 'Copyright 2015 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_98cd9320599b4ed69c652f2da3603a1e()\"><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; R2014b<br><\/p>\r\n<\/div>\r\n<!--\r\n98cd9320599b4ed69c652f2da3603a1e ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/26919-deploypcode-source-dir--target-dir--varargin- |deploypcode|>\r\n% by <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/1672378 Sven>.\r\n%\r\n% Sven's no stranger to Pick of the Week. This time, he has created a tool\r\n% that allows you to easily deploy your MATLAB files as\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/protect-your-source-code.html#bsc58yj-2\r\n% P-files> (protected files). P-files are obfuscated versions of your\r\n% MATLAB files and are good for hiding the contents of your MATLAB source\r\n% code. Note, however, that P-files are not _encrypted_ and should not be\r\n% considered secure.\r\n%\r\n% Sven's |deploypcode| can take the contents of a particular folder and\r\n% P-code all of the MATLAB files, including files in subfolders. The\r\n% function provides a number of additional features, as well. Two of my\r\n% favorites are the ability to keep the help text and the ability to copy\r\n% additional files for easy deployment. For example, let's say that I have\r\n% some MATLAB files and a data file.\r\n\r\ndir analysis\r\n\r\n%%\r\n% I will P-code them and copy the additional data file (Data.csv) into a\r\n% different folder.\r\n\r\ndeploypcode('analysis','distrib','copyDirectStrings','.csv')\r\n\r\n%%\r\n% In my 'distrib' folder, I have the P-files, the corresponding MATLAB\r\n% files (which contain the help text), and my CSV file.\r\n\r\ndir distrib\r\n\r\n%%\r\n% For those of you who want to learn some good coding practices, take a\r\n% look at Sven's code. A few things to mention:\r\n%\r\n% * Thorough help. Just type |help deploypcode| and you'll learn everything\r\n% you need to know about the function.\r\n% * Plenty of error-checking. He guards against potential problems, such as\r\n% missing source or target folders.\r\n% * Robust input parsing. He uses\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/inputparser-class.html\r\n% |inputParser|> to deal with various optional arguments.\r\n% * Use of\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/oncleanup.html\r\n% |onCleanup|>. To ensure that some clean up tasks are performed, even in\r\n% the case of errors, he uses the |onCleanup| function.\r\n% \r\n% Finally, be sure to check out\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/packaging-guis-as-apps.html App\r\n% Packaging> and <https:\/\/www.mathworks.com\/help\/matlab\/creating-help.html\r\n% Toolbox Distribution> to learn about other ways to distribute your\r\n% programs.\r\n%\r\n% *Comments*\r\n%\r\n% Give this a try and let us know what you think\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=5890#respond here> or leave a\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/26919-deploypcode-source-dir--target-dir--varargin-#comments\r\n% comment> for Sven.\r\n\r\n##### SOURCE END ##### 98cd9320599b4ed69c652f2da3603a1e\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Jiro's pick this week is deploypcode by Sven.\r\n   \r\n   Sven's no stranger to Pick of the Week. This time, he has created a tool that allows you to easily deploy your MATLAB files\r\n      as... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2015\/03\/13\/deploying-p-code-files\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5890"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=5890"}],"version-history":[{"count":3,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5890\/revisions"}],"predecessor-version":[{"id":5893,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5890\/revisions\/5893"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5890"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5890"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5890"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}