欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 幼教 > 044_Standalone App in Matlab中发布独立应用

044_Standalone App in Matlab中发布独立应用

2025/9/21 0:12:50 来源:https://blog.csdn.net/withstand/article/details/144932496  浏览:    关键词:044_Standalone App in Matlab中发布独立应用

在这里插入图片描述

Matalb中应用部署

Matlab因为年头久远,所有的东西都积累了一大堆。就说是应用部署,Matlab 2023b至少有下面的几个技术线

  • Matlab Compiler技术线:产生独立App可执行程序或者网页应用
  • Simulink Compiler技术线:产生独立App可执行程序,或者FMU(Functional Mock-up Unit)
  • MATLAB Compiler SDK技术线:产生软件组件
  • MATLAB production Server技术线:产生Web服务、数据库和企业应用
  • MATLAB Web App Server技术线:产生Web应用,支持MATLAB和Simulink

这是大的技术线划分,实际上,MATLAB的应用可以部署为:

  • 独立运行的(桌面)应用
  • Web应用
  • Microsoft Excel插件
  • 大数据分析应用
  • 微服务
  • 企业应用和云服务
  • 打包成库,供其他语言调用(Java、.Net, Python,C/C++)

所以,别问什么事情在MATLAB中行不行,就问你想要什么。只要给够钱,牛马们都能干。

打钱热线:666-666-6666

简单的独立App例子

下面给出一个简单的命令行应用打包的例子,为了展示过程,我们先整一个最简单的TUI应用,然后打包成独立App。

这个应用的功能超级牛叉,应用可以接受0个或者1个字符串参数,输出你好世界,来自xxx。当提供参数时,这个xxx就是参数,否则就是默认值:MATLAB。

function hello(name)
argumentsname (1,1) string = 'MATLAB'
end
fprintf('Hello, World from %s!\n', name);
end

这个程序可不要小看,是业界鼎鼎大名的Hello World程序,所有致力于修仙的小卡拉米首先都要把这个玩意整出来。跟别说,我们MATLAB仙人还提供了默认参数的选项,顿时把这个冒烟测试的例子提升到它不应该有的高度。

编译

首先,我们要把这个程序编译成可执行程序。请各位仙路上的同学们打开MATLAB,然后输入下面的命令:

help compiler.build.standaloneApplication

这个命令接受一个MATLAB函数文件的文件名作为参数,然后编译出可执行程序。当我们的函数文件是hello.m时,我们可以这样调用:

compiler.build.standaloneApplication('hello.m')

这个程序会在当前目录下申城一个hellostandaloneApplication的文件夹,里面包含了编译好的可执行程序:hello.exe(Windows)。

此外,这个目录里大概包含以下文件:

  • hello.exe:可执行程序hello.exe
  • includeSupportPackages:支持包列表,这里什么包都没有,字节0
  • readme.txt:说明文件
  • mccExcludedFiles.log:编译时排除的文件列表
  • requiredMCRProducts.txt:所需要的MATLAB Runtime产品
  • unresolvedSymbols.log:编译时未解决的符号列表
hello Executable1. Prerequisites for Deployment Verify that MATLAB Runtime(R2023b) is installed.   
If not, you can run the MATLAB Runtime installer.
To find its location, enter>>mcrinstallerat the MATLAB prompt.
NOTE: You will need administrator rights to run the MATLAB Runtime installer. Alternatively, download and install the Windows version of the MATLAB Runtime for R2023b 
from the following link on the MathWorks website:https://www.mathworks.com/products/compiler/mcr/index.htmlFor more information about the MATLAB Runtime and the MATLAB Runtime installer, see 
"Distribute Applications" in the MATLAB Compiler documentation  
in the MathWorks Documentation Center.2. Files to Deploy and PackageFiles to Package for Standalone 
================================
-hello.exe
-MCRInstaller.exe Note: if end users are unable to download the MATLAB Runtime using theinstructions in the previous section, include it when building your component by clicking the "Runtime included in package" link in theDeployment Tool.
-This readme file 3. DefinitionsFor information on deployment terminology, go to
https://www.mathworks.com/help and select MATLAB Compiler >
Getting Started > About Application Deployment >
Deployment Product Terms in the MathWorks Documentation
Center.

这个readme.txt文件里面包含了一些关于这个应用的信息,比如如何安装MATLAB Runtime,如何运行这个应用等等。

这里最重要的就是安装这个MCR,我们可以从requiredMCRProducts.txt中产看版本信息,然后去MATLAB官网下载对应的MCR安装包。

35010	

其他文件信息不再赘述。

安装包

当然,我们可以根据上面的readme.txt来安装这个应用,但是这个过程太繁琐了,我们可以把这个应用打包成一个安装包,然后一键安装。

这里我们就需要另外一个工具。修仙的朋友们请输入:

help compiler.package.installer

我们可以调用这个函数来产生一个安装包,这个包里最重要的参数有两个:

  • 第一个不具名参数:要打包的应用,直接用前面编译好的结果就行
  • RuntimeDelivery:MATLAB Runtime的安装包的提供方式,可以是web,可以是installer

当选择web时,这个安装包会包含一个URL,用户安装时会提示从这个URL下载MCR安装包。当选择installer时,这个安装包会包含MCR安装包,用户可以直接安装。

appFile = "hello.m";
buildResults = compiler.build.standaloneApplication(appFile);% >> buildResults
% buildResults =
%   Results - 属性:%                   BuildType: 'standaloneApplication'
%                       Files: {2x1 cell}
%     IncludedSupportPackages: {}
%                     Options: [1x1 compiler.build.StandaloneApplicationOptions]%% package the standalone applicationcompiler.package.installer(buildResults, ...'InstallerName', 'HelloInstall', ...'RuntimeDelivery', 'web'...);compiler.package.installer(buildResults, ...'InstallerName', 'HelloInstallWithRuntime', ...'RuntimeDelivery', 'installer'...);

这里我们展示了两种调用方式,web的方式会产生一个很小的安装文件:

  • helloInstaller.exe

另外一种方式会产生一个接近500M的玩意,包含了所需要的MCR安装内容,直接安装即可使用。

这两个文件的大小参见:

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  1. 首先,一定要相信MATLAB,它能做到一切,但是得加钱!
  2. 其次,MATLAB的应用部署技术线很多,不同的应用场景有不同的选择。
  3. 这个TUI的可执行文件还很小的,只有1.2M,用起来也很方便。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词