Main Content

写可部署MATLAB代码

包装应用需要功能

实施的申请MATLAB®编译器SDK™andMATLAB生产服务器™通过MATLAB函数生成的API访问MATLAB代码。所有用于在这些应用程序中使用的MATLAB代码都必须写为MATLAB函数。

包装应用程序不处理MATLAB运行时文件

这compiler secures your code against unauthorized changes. Deployable MATLAB files are suspended or frozen at the time of compilation. This does not mean that you cannot deploy a flexible application—it means thatyou must design your application with flexibility in mind。If you want the end user to be able to choose between two different methods, for example, both methods must be available in the deployable archive.

MATLABRuntimeonly works on MATLAB code that was encrypted when the deployable archive was built. Any function or process that dynamically generates new MATLAB code will not work againstMATLABRuntime

一些MATLAB工具箱,例如深学习Toolbox™产品,会动态生成MATLAB代码。因为MATLABRuntime仅执行加密的MATLAB文件,而深度学习工具箱会生成未加密的MATLAB文件,深度学习工具箱中的某些功能无法部署。

同样,无法部署需要检查MATLAB功能文件内容的功能。HELP, for example, is dynamic and not available in deployed mode. You can use载荷in deployed mode if you provide it with a MATLAB function prototype.

而不是编译生成MATLAB代码并尝试部署它的函数,而是执行以下任务:

  1. 在MATLAB中运行一次代码以获取您的生成功能。

  2. 包装MATLAB代码,包括生成的功能。

提示

使用的另一种选择评估orFeval正在使用匿名函数手柄。

如果您需要创建用于动态运行时处理的MATLAB代码的能力,则最终用户必须具有MATLAB的安装副本。

不要依靠更改目录或路径来控制执行MATLAB文件

通常,良好的编程实践建议不要在代码中动态地重定向程序搜索路径。许多开发人员倾向于这种行为,因为它模仿了他们通常在命令行中执行的操作。但是,部署代码时可能会导致问题。

For example, in a deployed application, the MATLAB and Java®路径是固定的,无法改变。因此,任何改变这些路径的尝试(使用光盘命令或addpath命令)失败。

If you find you cannot avoid placingaddpath在您的MATLAB代码中呼叫,使用ismccandiSdeployed。See使用ISDEPLOYED函数执行特定部署的代码路径有关详细信息。

使用ISDEPLOYED函数执行特定部署的代码路径

iSdeployedfunction allows you to specify which portion of your MATLAB code is deployable, and which is not. Such specification minimizes your compilation errors and helps create more efficient, maintainable code.

For example, you find it unavoidable to useaddpath写你的startup.m。使用ismccandiSdeployed什么是包装和时,你指定executed.

如果〜(ismcc || isDeployed)addpath(mypath);结尾

逐渐取决于不可限制功能的重构应用

随着时间的流逝,重构,精简和模块化MATLAB代码,包含使用的不可兼容或不可剥削的功能iSdeployed。您最终的目标是“优雅的降级”,对不可部署的代码。换句话说,该代码必须向最终用户提供尽可能少的部署障碍,直到实际消除。

Partition your code into design-time and run-time code sections:

  • Design-time code是当前正在发展的代码。几乎所有代码都经历了永久重写,调试和优化的阶段。在某些工具箱(例如深度学习工具箱产品)中,该代码会经历一段时间的自我训练,因为它对各种数据排列和模式做出了反应。这种代码几乎从未设计为部署。

  • 运行时代码, on the other hand, has solidified or become stable—it is in a finished state and is ready to be deployed by the end user.

考虑为代码创建单独的目录,该目录不应部署或调用不可剥削的代码的代码。

Do Not Create or Use Nonconstant Static State Variables

避免使用以下内容:

  • Global variables in MATLAB code

  • MEX文件中的静态变量

  • Static variables in Java code

这些变量的状态是持久的,并与过程中的所有内容共享。

部署应用程序时,使用持久变量可能会引起问题,因为MATLABRuntime流程以单个线程运行。您不能将这些非恒定的静态变量加载到同一过程中。此外,这些静态变量在多线程应用程序中不能很好地工作。

When programming against packaged MATLAB code, you should be aware that an instance ofMATLABRuntimeis created for each instance of a new class. If the same class is instantiated again using a different variable name, it is attached to theMATLABRuntime实例由同一类的上一个实例创建。简而言之,如果一个组件包含n独特的课程,最多n实例MATLABRuntimecreated, each corresponding to one or more instances of one of the classes.

If you must use static variables, bind them to instances. For example, defining instance variables in a Java class is preferable to defining the variable as静止的

Get Proper Licenses for Toolbox Functionality You Want to Deploy

You must have a valid MathWorks®license for toolboxes you use to create deployable MATLAB code.

也可以看看

|

Related Topics