{"id":142,"date":"2008-06-19T06:35:55","date_gmt":"2008-06-19T11:35:55","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2008\/06\/19\/writing-deployable-code\/"},"modified":"2016-07-29T16:41:49","modified_gmt":"2016-07-29T21:41:49","slug":"writing-deployable-code","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2008\/06\/19\/writing-deployable-code\/","title":{"rendered":"Writing Deployable Code"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>I'm pleased to introduce <a href=\"mailto:pwebb@mathworks.com\">Peter Webb<\/a> as this week's guest blogger. Peter has worked on the MATLAB Compiler since 1995. This is the first in a series of posts\r\n         about how to make your M-files compile more easily.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#2\">General Guidelines<\/a><\/li>\r\n         <li><a href=\"#10\">Finding Functions at Compile Time<\/a><\/li>\r\n         <li><a href=\"#11\">Non-deployable Functions<\/a><\/li>\r\n         <li><a href=\"#13\">Detecting Dependencies on Callback Functions<\/a><\/li>\r\n         <li><a href=\"#18\">Including Functions Called by eval<\/a><\/li>\r\n         <li><a href=\"#20\">Keep Reading<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>The <a href=\"https:\/\/www.mathworks.com\/products\/compiler\">MATLAB Compiler<\/a> and the deployment tools create deployable applications by \"freezing\" a collection of MATLAB functions and state information into a portable package.\r\n      Deployed applications run using the MATLAB Component Runtime (MCR), which \"thaws\" the functions and state back into executable\r\n      form. The Compiler creates either standalone executables or shared libraries (DLLs). Many of the differences between MATLAB\r\n      and the MCR arise because a single user-written application may make use of multiple Compiler-generated shared libraries;\r\n      in this case the shared libraries share the MCR's global data.\r\n   <\/p>\r\n   <p>The MCR differs from MATLAB in five ways:<\/p>\r\n   <div>\r\n      <ul>\r\n         <li>The MCR has no desktop.<\/li>\r\n         <li>The MCR only executes encrypted M-files.<\/li>\r\n         <li>Global MCR resources, like the Handle Graphics root object and the   random number seed, are shared between multiple libraries\r\n            working   together.\r\n         <\/li>\r\n         <li>The M-File and Java paths are fixed and cannot be changed.<\/li>\r\n         <li>Certain MATLAB and toolbox features are not available to   deployed applications.<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Because the MCR differs from MATLAB, M-Files written to run using the MCR may sometimes differ from M-Files that run only\r\n      in MATLAB. Generally speaking, the differences between MATLAB and the MCR fall into four categories:\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>Finding functions at compile time<\/li>\r\n         <li>Path management at runtime<\/li>\r\n         <li>Non-deployable features<\/li>\r\n         <li>Differences in behavior<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>This article describes some general guidelines and then focuses on the problem of finding functions at compile time; I will\r\n      address the other three types of differences in subsequent posts.\r\n   <\/p>\r\n   <h3>General Guidelines<a name=\"2\"><\/a><\/h3>\r\n   <p>The process of compiling and deploying an application is more efficient if the application follows these guidelines.<\/p>\r\n   <div>\r\n      <ul>\r\n         <li> Don't create or use non-constant static state anywhere    (in M, C++ or Java code). Code that uses global variables, for\r\n               example, will likely yield unexpected results if other applications    share the MCR's global resources. <b>Don't do this<\/b>:\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">    <span style=\"color: #0000FF\">global<\/span> data;\r\n    y = data + x;\r\n    data = x;<\/pre><p><html><div><ul style=\"list-style: none;\"><li> Instead, create a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/handle-classes.html\">MATLAB\r\n      handle object<\/a> to store the shared data, and pass that object to each function that needs access to the shared data. <\/li><\/ul><\/div><\/html>\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>Compiled applications cannot read, parse, create or manipulate   M-files at runtime. The MCR will only execute encrypted M-Files;\r\n            as   a result, all of the M-files in a deployed application are encrypted.   MATLAB functions like   <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/fopen.html\"><tt>fopen<\/tt><\/a>   and <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/help.html\"><tt>help<\/tt><\/a>   will read encrypted M-Files as unintelligible binary data. Deployed   applications cannot, for example, create and then\r\n            execute M-files at   runtime. <b>This will compile, but won't run<\/b>:\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">    fp = fopen(<span style=\"color: #A020F0\">'Compute.m'<\/span>, <span style=\"color: #A020F0\">'w'<\/span>);\r\n    fprintf(fp, <span style=\"color: #A020F0\">'function x = Compute(y)\\n'<\/span>);\r\n    fprintf(fp, <span style=\"color: #A020F0\">'    x = y + 17;\\n'<\/span>);\r\n    fclose(fp);\r\n    rehash;\r\n    z = Compute(10);<\/pre><p><html><div><ul style=\"list-style: none;\"><li> This behavior is by design. You must ensure that your application contains all\r\n      the M-files it needs at compile time. <\/li><\/ul><\/div><\/html>\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>Don't rely on the current directory   (<a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/cd.html\"><tt>cd<\/tt><\/a>)   or changes to the MATLAB path   (<a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/addpath.html\"><tt>addpath<\/tt><\/a>)   to control the execution of M-files. The path of a   deployed application is determined at compiled time, and remains\r\n              forever fixed. The current directory is <b>never<\/b> on the path in a   deployed application. In the code below, <tt>cd<\/tt>-ing to the <tt>stringFcns<\/tt>   directory does not make <tt>stringFcns\/add<\/tt> shadow <tt>mathFcns\/add<\/tt>.   The order of these functions at runtime   depends on what their order was at compile time. <b>This causes errors:<\/b><\/li>\r\n      <\/ul>\r\n   <\/div><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">    cd <span style=\"color: #A020F0\">mathFcns<\/span>\r\n    z = add(x, y);\r\n    cd <span style=\"color: #A020F0\">..\/stringFcns<\/span>\r\n    s = add(s1, s2);<\/pre><p><html><div><ul style=\"list-style: none;\"><li> Avoid this problem by either using different function names, e.g., <tt>addstring<\/tt>\r\n      and <tt>addnum<\/tt> or <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/object-oriented-programming.html\">MATLAB\r\n      Objects<\/a>. <\/li><\/ul><\/div><\/html>\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>Use the   <a href=\"https:\/\/www.mathworks.com\/access\/helpdesk\/help\/toolbox\/compiler\/isdeployed.html\"><tt>isdeployed<\/tt><\/a>   function (available in M, C++ and Java) to execute deployment   specific code paths, or to protect MATLAB-only   code (<tt>~isdeployed<\/tt>). <tt>isdeployed<\/tt> is true when run in the MCR, and   false when run in MATLAB. For example, deployed applications must   use  <tt>deployprint<\/tt>,   rather than   <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/print.html\"><tt>print<\/tt><\/a>,   to send data to the printer:\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">    <span style=\"color: #0000FF\">if<\/span> ~isdeployed\r\n        print\r\n    <span style=\"color: #0000FF\">else<\/span>\r\n        deployprint\r\n    <span style=\"color: #0000FF\">end<\/span><\/pre><div>\r\n      <ul>\r\n         <li>Provide graceful degradation for applications that rely on   non-deployable functions. Typically, this means using <tt>isdeployed<\/tt>   to provide deployable alternatives to non-deployable functions, or   at least to issue a precise warning message.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">    <span style=\"color: #0000FF\">switch<\/span> (userInput)\r\n        <span style=\"color: #228B22\">% There's no editor available to deployed applications;<\/span>\r\n        <span style=\"color: #228B22\">% print a warning message.<\/span>\r\n        <span style=\"color: #0000FF\">case<\/span> <span style=\"color: #A020F0\">'EditMFile'<\/span>\r\n            <span style=\"color: #0000FF\">if<\/span> isdeployed\r\n                warning(<span style=\"color: #A020F0\">'MyApp:NonDeployable'<\/span>, <span style=\"color: #0000FF\">...<\/span>\r\n                    [<span style=\"color: #A020F0\">'The editor is not available '<\/span>, <span style=\"color: #0000FF\">...<\/span>\r\n                    <span style=\"color: #A020F0\">'in compiled applications.'<\/span>]);\r\n            <span style=\"color: #0000FF\">else<\/span>\r\n                edit(mfile);\r\n            <span style=\"color: #0000FF\">end<\/span>\r\n        <span style=\"color: #228B22\">% Deployed applications can't call DOC. But they can<\/span>\r\n        <span style=\"color: #228B22\">%  redirect a help query to The MathWorks help site.<\/span>\r\n        <span style=\"color: #0000FF\">case<\/span> <span style=\"color: #A020F0\">'Help'<\/span>\r\n            <span style=\"color: #0000FF\">if<\/span> ~isdeployed\r\n                doc(mfile);\r\n            <span style=\"color: #0000FF\">else<\/span>\r\n                web(<span style=\"color: #A020F0\">'https:\/\/www.mathworks.com\/support.html'<\/span>);\r\n            <span style=\"color: #0000FF\">end<\/span>\r\n    <span style=\"color: #0000FF\">end<\/span><\/pre><h3>Finding Functions at Compile Time<a name=\"10\"><\/a><\/h3>\r\n   <p>In order to produce a deployable package that is smaller than a MATLAB installation, the MATLAB Compiler attempts to narrow\r\n      the set of deployed functions to only those required by the application. The Compiler calls the MATLAB function <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.codetools.requiredfilesandproducts.html\"><tt>depfun<\/tt><\/a> to compute the <i>dependent functions<\/i> of each M-File it compiles. This process differs from the mechanism by which MATLAB determines which functions to call.\r\n   <\/p>\r\n   <p>MATLAB searches for the best match for a function at runtime, when the types of the inputs and the contents of the MATLAB\r\n      path are known precisely. <tt>depfun<\/tt> analyzes M-files at compile-time, when much less information is available; as a result it may omit functions that your application\r\n      requires. Functions may be omitted because they are non-deployable or invoked only as callbacks or by <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/eval.html\"><tt>eval<\/tt><\/a>.\r\n   <\/p>\r\n   <h3>Non-deployable Functions<a name=\"11\"><\/a><\/h3>\r\n   <p>Licensing restrictions prevent most <i>design time<\/i> functions from being compiled or deployed. A design time function is one that changes or augments the basic functionality\r\n      of a program. Design time functions include the MATLAB editor, most toolbox GUI-based tools, like the <a href=\"https:\/\/www.mathworks.com\/products\/image\">Image Processing Toolbox<\/a> function <tt>imtool<\/tt>, and functions that create M-Files, like the <a href=\"https:\/\/www.mathworks.com\/products\/fuzzylogic\">Fuzzy Logic Toolbox<\/a> function <a href=\"https:\/\/www.mathworks.com\/help\/fuzzy\/anfis.html\"><tt>anfis<\/tt><\/a>. <b>This code will compile, but won't run<\/b>:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">    [fis,error,stepsize] = anfis(trainingData);<\/pre><p>Attempting to execute non-deployable functions generates <tt>MATLAB:UndefinedFunction<\/tt> errors at runtime. Check your application's code to make sure it uses only <a href=\"https:\/\/www.mathworks.com\/products\/compiler\/compiler_support.html\">deployable functions<\/a>. <tt>mcc<\/tt> generates a list of <i>excluded functions<\/i> into a file called <tt>mccExcludedFunctions.log<\/tt>. Search this file for the string \"because of compilability rules\" to find the non-deployable functions <tt>depfun<\/tt> excluded from compilation. Because of the conservative nature of <tt>depfun<\/tt>'s compile-time analysis, <tt>mccExcludedFiles.log<\/tt> may be quite large, and may list functions that your application does not require.\r\n   <\/p>\r\n   <h3>Detecting Dependencies on Callback Functions<a name=\"13\"><\/a><\/h3>\r\n   <p>The MATLAB Compiler's dependency analysis statically analyzes M-file functions to determine what other M-files they depend\r\n      on. This analysis cannot determine the types of any variables or examine the contents of any strings. As a result, callback\r\n      functions, which are often mentioned only in strings, can be overlooked; the Compiler will produce an application that appears\r\n      to work but which will generate runtime errors when it attempts to invoke the missing callbacks.\r\n   <\/p>\r\n   <p>For example:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">set(gca, <span style=\"color: #A020F0\">'ButtonDownFcn'<\/span>, <span style=\"color: #A020F0\">'LogButtonPress'<\/span>);<\/pre><p>If <tt>LogButtonPress<\/tt> is not included in the deployed package, this program will fail. Include <tt>LogButtonPress<\/tt> by explicitly specifying it on the <a href=\"https:\/\/www.mathworks.com\/access\/helpdesk\/help\/toolbox\/compiler\/mcc.html\"><tt>mcc<\/tt><\/a> command line, or inserting a <a href=\"https:\/\/www.mathworks.com\/access\/helpdesk\/help\/toolbox\/compiler\/function.html\"><tt>%#function<\/tt><\/a> pragma in the M-file that uses <tt>LogButtonPress<\/tt>.\r\n   <\/p>\r\n   <p>Specifying a callback using <tt>mcc<\/tt>:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">mcc <span style=\"color: #A020F0\">[other options]<\/span> <span style=\"color: #A020F0\">-a<\/span> <span style=\"color: #A020F0\">dir1\/dir2\/LogButtonPress.m<\/span><\/pre><p>Using a <tt>%#function<\/tt> pragma\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">%#function LogButtonPress<\/span><\/pre><h3>Including Functions Called by eval<a name=\"18\"><\/a><\/h3>\r\n   <p>MATLAB's <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/eval.html\"><tt>eval<\/tt><\/a> function poses the same kind of problem for the Compiler's dependency analysis that callbacks do: the Compiler cannot examine\r\n      text strings to determine if they contain function calls. Applications that use <tt>eval<\/tt> fail at runtime if the functions invoked by <tt>eval<\/tt> are not included in the deployed package.\r\n   <\/p>\r\n   <p>For example, this code that uses <tt>eval<\/tt> to create dynamically named MATLAB variables requires the function <tt>getdata<\/tt>:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">for<\/span> n = 1:count\r\n   eval(sprintf(<span style=\"color: #A020F0\">'x%d = getdata(%d);'<\/span>, n, n));\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><p>The solutions to problems created by <tt>eval<\/tt> are the same as those for callbacks: use the <tt>%#function<\/tt> pragma, or explicitly deploy the function in question by listing it as an argument to your <tt>mcc<\/tt> command.\r\n   <\/p>\r\n   <h3>Keep Reading<a name=\"20\"><\/a><\/h3>\r\n   <p>I plan to address issues and workarounds surrounding runtime path management, non-supported functions and differences in function\r\n      behavior in future posts. In the meantime, you can refer to the documentation for the MATLAB Compiler or post follow-up questions <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=142#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_3641a0b550764dd6857693a5b18e2ddd() {\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='3641a0b550764dd6857693a5b18e2ddd ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 3641a0b550764dd6857693a5b18e2ddd';\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 2008 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_3641a0b550764dd6857693a5b18e2ddd()\"><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.6<br><\/p>\r\n<\/div>\r\n<!--\r\n3641a0b550764dd6857693a5b18e2ddd ##### SOURCE BEGIN #####\r\n%% Writing Deployable Code\r\n% I'm pleased to introduce <mailto:pwebb@mathworks.com Peter Webb> as \r\n% this week's guest blogger. Peter has worked on the MATLAB Compiler\r\n% since 1995. This is the first in a series of posts about how to make your\r\n% M-files compile more easily.\r\n\r\n%%\r\n% The \r\n% <https:\/\/www.mathworks.com\/products\/compiler MATLAB Compiler>\r\n% and the \r\n% <https:\/\/www.mathworks.com\/solutions\/technical-computing.htmldescription\/deploy.html deployment tools>\r\n% create deployable applications by  \r\n% \"freezing\" a collection of MATLAB functions and state information into\r\n% a portable package. Deployed applications run using the MATLAB \r\n% Component Runtime (MCR), which \"thaws\" the functions and state back \r\n% into executable form. The Compiler creates either standalone\r\n% executables or shared libraries (DLLs). Many of the differences between\r\n% MATLAB and the MCR arise because a single user-written application may\r\n% make use of multiple Compiler-generated shared libraries; in this case the\r\n% shared libraries share the MCR's global data.\r\n%\r\n% The MCR differs from MATLAB in five ways:\r\n%\r\n% * The MCR has no desktop.\r\n% * The MCR only executes encrypted M-files.\r\n% * Global MCR resources, like the Handle Graphics root object and the\r\n%   random number seed, are shared between multiple libraries working\r\n%   together.\r\n% * The M-File and Java paths are fixed and cannot be changed.\r\n% * Certain MATLAB and toolbox features are not available to\r\n%   deployed applications.\r\n%\r\n% Because the MCR differs from MATLAB, M-Files written to run using\r\n% the MCR may sometimes differ from M-Files that run only in MATLAB. \r\n% Generally speaking, the differences between MATLAB and the MCR fall\r\n% into four categories: \r\n%\r\n% * Finding functions at compile time\r\n% * Path management at runtime\r\n% * Non-deployable features\r\n% * Differences in behavior\r\n%\r\n% This article describes some general guidelines and then focuses on the \r\n% problem of finding functions at compile time; I will address the other \r\n% three types of differences in subsequent posts.\r\n\r\n%% General Guidelines\r\n% The process of compiling and deploying an application is more \r\n% efficient if the application follows these guidelines. \r\n%\r\n% *  Don't create or use non-constant static state anywhere \r\n%    (in M, C++ or Java code). Code that uses global variables, for \r\n%    example, will likely yield unexpected results if other applications \r\n%    share the MCR's global resources. *Don't do this*:\r\n    global data;\r\n    y = data + x;\r\n    data = x;\r\n%%\r\n% <html><div><ul style=\"list-style: none;\"><li>\r\n% Instead, create a <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/handle-classes.html\">MATLAB handle object<\/a>\r\n% to store the shared data, and pass that object to each function that\r\n% needs access to the shared data.\r\n% <\/li><\/ul><\/div><\/html>\r\n    \r\n%%    \r\n%\r\n% * Compiled applications cannot read, parse, create or manipulate \r\n%   M-files at runtime. The MCR will only execute encrypted M-Files; as\r\n%   a result, all of the M-files in a deployed application are encrypted.\r\n%   MATLAB functions like \r\n%   <https:\/\/www.mathworks.com\/help\/matlab\/ref\/fopen.html |fopen|> \r\n%   and <https:\/\/www.mathworks.com\/help\/matlab\/ref\/help.html |help|>  \r\n%   will read encrypted M-Files as unintelligible binary data. Deployed \r\n%   applications cannot, for example, create and then execute M-files at\r\n%   runtime. *This will compile, but won't run*:\r\n% \r\n    fp = fopen('Compute.m', 'w');\r\n    fprintf(fp, 'function x = Compute(y)\\n');\r\n    fprintf(fp, '    x = y + 17;\\n');\r\n    fclose(fp);\r\n    rehash;\r\n    z = Compute(10);\r\n%%\r\n% <html><div><ul style=\"list-style: none;\"><li>\r\n% This behavior is by design. You must ensure that your application \r\n% contains all the M-files it needs at compile time.\r\n% <\/li><\/ul><\/div><\/html>    \r\n%%\r\n% * Don't rely on the current directory \r\n%   (<https:\/\/www.mathworks.com\/help\/matlab\/ref\/cd.html |cd|>) \r\n%   or changes to the MATLAB path \r\n%   (<https:\/\/www.mathworks.com\/help\/matlab\/ref\/addpath.html |addpath|>)\r\n%   to control the execution of M-files. The path of a \r\n%   deployed application is determined at compiled time, and remains\r\n%   forever fixed. The current directory is *never* on the path in a \r\n%   deployed application. In the code below, |cd|-ing to the |stringFcns| \r\n%   directory does not make |stringFcns\/add| shadow |mathFcns\/add|. \r\n%   The order of these functions at runtime \r\n%   depends on what their order was at compile time. *This causes errors:*\r\n    cd mathFcns\r\n    z = add(x, y);\r\n    cd ..\/stringFcns\r\n    s = add(s1, s2);\r\n%%\r\n% <html><div><ul style=\"list-style: none;\"><li>\r\n% Avoid this problem by either using different function names, e.g., \r\n% <tt>addstring<\/tt> and <tt>addnum<\/tt> or <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/object-oriented-programming.html\">MATLAB Objects<\/a>.\r\n% <\/li><\/ul><\/div><\/html>\r\n\r\n%%\r\n% * Use the \r\n%   <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/toolbox\/compiler\/isdeployed.html |isdeployed|>\r\n%   function (available in M, C++ and Java) to execute deployment \r\n%   specific code paths, or to protect MATLAB-only \r\n%   code (|~isdeployed|). |isdeployed| is true when run in the MCR, and\r\n%   false when run in MATLAB. For example, deployed applications must \r\n%   use \r\n%   <https:\/\/www.mathworks.com\/help\/compiler\/index.htmldeployprint.html |deployprint|>,\r\n%   rather than \r\n%   <https:\/\/www.mathworks.com\/help\/matlab\/ref\/print.html |print|>,\r\n%   to send data to the printer: \r\n    if ~isdeployed\r\n        print\r\n    else\r\n        deployprint\r\n    end\r\n%%\r\n% * Provide graceful degradation for applications that rely on \r\n%   non-deployable functions. Typically, this means using |isdeployed|\r\n%   to provide deployable alternatives to non-deployable functions, or\r\n%   at least to issue a precise warning message.\r\n    switch (userInput)\r\n        % There's no editor available to deployed applications;\r\n        % print a warning message.\r\n        case 'EditMFile'\r\n            if isdeployed\r\n                warning('MyApp:NonDeployable', ...\r\n                    ['The editor is not available ', ...\r\n                    'in compiled applications.']);\r\n            else\r\n                edit(mfile);\r\n            end\r\n        % Deployed applications can't call DOC. But they can\r\n        %  redirect a help query to The MathWorks help site.\r\n        case 'Help'\r\n            if ~isdeployed\r\n                doc(mfile);\r\n            else\r\n                web('https:\/\/www.mathworks.com\/support.html');\r\n            end\r\n    end\r\n%% Finding Functions at Compile Time\r\n% In order to produce a deployable package that is smaller than a\r\n% MATLAB installation, the MATLAB Compiler attempts to narrow the set\r\n% of deployed functions to only those required by the application. The \r\n% Compiler calls the MATLAB function <https:\/\/www.mathworks.com\/help\/matlab\/ref\/matlab.codetools.requiredfilesandproducts.html |depfun|>\r\n% to compute the _dependent functions_ of each M-File it compiles. This \r\n% process differs from the mechanism by which MATLAB determines which \r\n% functions to call.\r\n%\r\n% MATLAB searches for the best match for a function at runtime, when the\r\n% types of the inputs and the contents of the MATLAB path are known \r\n% precisely. |depfun| analyzes M-files at compile-time, when much less\r\n% information is available; as a result it may omit functions that your\r\n% application requires. Functions may be omitted because they are \r\n% non-deployable or invoked only as callbacks or by\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/eval.html\r\n% |eval|>.\r\n\r\n%% Non-deployable Functions\r\n% Licensing restrictions prevent most _design time_ functions from being\r\n% compiled or deployed. A design time function is one that changes or\r\n% augments the basic functionality of a program. Design time functions\r\n% include the MATLAB editor, most toolbox GUI-based tools, like the \r\n% <https:\/\/www.mathworks.com\/products\/image Image Processing Toolbox> function \r\n% <https:\/\/www.mathworks.com\/help\/images\/index.htmlimtool.html\r\n% |imtool|>, and functions that create M-Files, like the \r\n% <https:\/\/www.mathworks.com\/products\/fuzzylogic Fuzzy Logic Toolbox> function \r\n% <https:\/\/www.mathworks.com\/help\/fuzzy\/anfis.html |anfis|>.\r\n% *This code will compile, but won't run*:\r\n    [fis,error,stepsize] = anfis(trainingData);\r\n%%\r\n% Attempting to execute non-deployable functions generates\r\n% <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/matlab_prog\/bq9l448-1.html |MATLAB:UndefinedFunction|>\r\n% errors at runtime. Check your application's\r\n% code to make sure it uses only\r\n% <https:\/\/www.mathworks.com\/products\/compiler\/compiler_support.html\r\n% deployable functions>. |mcc| generates a list of \r\n% <https:\/\/www.mathworks.com\/help\/compiler\/index.htmlbqrvu87-6.html \r\n% _excluded functions_>\r\n% into a file called |mccExcludedFunctions.log|. Search this file for the\r\n% string \"because of compilability rules\" to find the non-deployable \r\n% functions |depfun| excluded from compilation. Because\r\n% of the conservative nature of |depfun|'s compile-time analysis, \r\n% |mccExcludedFiles.log| may be quite large, and may list functions that\r\n% your application does not require. \r\n\r\n%% Detecting Dependencies on Callback Functions\r\n% The MATLAB Compiler's dependency analysis statically analyzes M-file \r\n% functions to determine what other M-files they depend on. This analysis \r\n% cannot determine the types of any variables or examine the contents of \r\n% any strings. As a result, callback functions, which are often mentioned \r\n% only in strings, can be overlooked; the Compiler will produce an \r\n% application that appears to work but which will generate runtime errors \r\n% when it attempts to invoke the missing callbacks.\r\n%\r\n% For example: \r\nset(gca, 'ButtonDownFcn', 'LogButtonPress');\r\n%%\r\n% If |LogButtonPress| is not included in the deployed package, this program \r\n% will fail. Include |LogButtonPress| by explicitly specifying it on the \r\n% <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/toolbox\/compiler\/mcc.html |mcc|> command \r\n% line, or inserting a \r\n% <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/toolbox\/compiler\/function.html |%#function|>\r\n% pragma in the M-file that uses |LogButtonPress|.\r\n\r\n%%\r\n% Specifying a callback using |mcc|:\r\nmcc [other options] -a dir1\/dir2\/LogButtonPress.m\r\n\r\n%%\r\n% Using a |%#function| pragma\r\n%#function LogButtonPress\r\n\r\n%% Including Functions Called by |eval|\r\n% MATLAB's \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/eval.html |eval|>\r\n% function poses the same kind of problem for the Compiler's dependency\r\n% analysis that callbacks do: the Compiler cannot examine text strings\r\n% to determine if they contain function calls. Applications that use |eval|\r\n% fail at runtime if the functions invoked by |eval| are not included\r\n% in the deployed package.\r\n%\r\n% For example, this code that uses |eval| to create dynamically named\r\n% MATLAB variables requires the function |getdata|:\r\nfor n = 1:count    \r\n   eval(sprintf('x%d = getdata(%d);', n, n));\r\nend\r\n\r\n%%\r\n% The solutions to problems created by |eval| are the same as those for\r\n% callbacks: use the |%#function| pragma, or explicitly deploy the function\r\n% in question by listing it as an argument to your |mcc| command.\r\n\r\n%% Keep Reading\r\n% I plan to address issues and workarounds surrounding runtime\r\n% path management, non-supported functions and differences in function\r\n% behavior in future posts. In the meantime, you can refer to the \r\n% <https:\/\/www.mathworks.com\/help\/compiler\/index.htmlbq6ae_n-1.html documentation>\r\n% for the MATLAB Compiler or post follow-up questions \r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=142#respond here>.\r\n\r\n##### SOURCE END ##### 3641a0b550764dd6857693a5b18e2ddd\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      I'm pleased to introduce Peter Webb as this week's guest blogger. Peter has worked on the MATLAB Compiler since 1995. This is the first in a series of posts\r\n         about how to... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2008\/06\/19\/writing-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":[16,24],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/142"}],"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=142"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/142\/revisions"}],"predecessor-version":[{"id":1829,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/142\/revisions\/1829"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}