Matlab之GUI生成EXE文件


 

原文:https://www.cnblogs.com/airbird/p/6160260.html

目录

近期因为项目需要,简化流程,写了一些Matlab程序,并配备上了GUI界面使其简单易用。然后问题来了,可移植性。使用Matlab生成EXE文件(可以封装很多的function),然后在一台安装有Matlab Runtime环境的电脑上运行,是一种不错的选择。

本文主要就我自己在GUI生成EXE文件上遇到的一些问题以及解决办法进行一个说明,希望可以帮助到有同样需求的人。

回到顶部

配置环境

  • Windows 10系统
  • Matlab 2013a
  • Visual Studio 2013

回到顶部

配置方法

打开Matlab,在命令行中输入

1

mbuild -setup

 

进行编译环境的配置,将会出现如下的界面。

1

2

3

4

5

6

7

Welcome to mbuild -setup. This utility will help you set up

a default compiler. For a list of supported compilers, see

http://www.mathworks.com/support/compilers/R2013a/win64.html

 

Please choose your compiler for building shared libraries or COM components:

 

Would you like mbuild to locate installed compilers [y]/n?

 

输入y确认后观察是否有编译器的存在,我的一开始是没有的,如果你的有matlab自带的lcc,那么幸运的是,你不用麻烦安装别的编译器了,这个已经足够,可以搜索与之相关的文章了;不幸的是后面的可能就不适合你了,因为我最后是使用的VS的编译器环境。

如果你也没有任何的编译器,那么很好,可以接着和我操作了,输入Ctrl+C退出当前命令,然后重新输入mbuild -setup。选择n,可以看到,出现了如下的一些选项。

1

2

3

4

5

6

7

Select a compiler:

[1] Microsoft Software Development Kit (SDK) 7.1

[2] Microsoft Visual C++ 2008 SP1

[3] Microsoft Visual C++ 2010

[4] Microsoft Visual C++ 2012

 

[0] None

 

Matlab 2013a中居然没有我安装的VS2013,伤心。不过想来也是,没有是正常的,那么后续怎么办呢,按照其中的安装呗,2008SP1,2010,2012都可以。博主在这里选择了VS2010,同一个电脑是可以安装不同版本的VS哦。

在VS2010完成后,重新启动Matlab,然后再次输入这条指令mbuild -setup,但是这次我们选择y,进入如下的界面,这次就可以看到我们安装的VS2010环境了,Nice!然后一路正常设置就好。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

Would you like mbuild to locate installed compilers [y]/n? y

 

Select a compiler:

[1] Microsoft Visual C++ 2010 in D:\Program Files (x86)\Microsoft Visual Studio 10.0

 

[0] None

 

Compiler: 1

 

Please verify your choices:

 

Compiler: Microsoft Visual C++ 2010

Location: D:\Program Files (x86)\Microsoft Visual Studio 10.0

 

Are these correct [y]/n? y

 

****************************************************************************

Warning: Applications/components generated using Microsoft Visual C++

2010 require that the Microsoft Visual Studio 2010 run-time

libraries be available on the computer used for deployment.

To redistribute your applications/components, be sure that the

deployment machine has these run-time libraries.

****************************************************************************

 

 

Trying to update options file: C:\Users\AirBird\AppData\Roaming\MathWorks\MATLAB\R2013a\compopts.bat

From template: D:\Program Files\MATLAB\R2013a\bin\win64\mbuildopts\msvc100compp.bat

 

Done . . .

 

至此,Matlab编译器配置完成。

回到顶部

生成EXE文件

在命令行中键入如下命令可以进行EXE文件的生成。

1

2

mcc -m myfile.m; % 只有.m文件时

mcc -m myfile.fig myfile.m; % .fig文件和.m文件一起时

但是,在运行生成的EXE文件时我们会发现有黑框的存在,这个黑框其实是作为控制台的存在,有什么信息可以打印到上面。但是很多时候我们在运行的时候不希望黑框的生成,那么该如何操作呢?这里只需要更改下命令就可以了,如下。

1

2

mcc -e myfile.m; % 只有.m文件时

mcc -e myfile.fig myfile.m; % .fig文件和.m文件一起时

 

其中-e是生成不带黑框的EXE程序,是不是很神奇。但是这里需要注意的是,-e的用法只适合VS的引擎。有关mcc的用法,可以help mcc或者doc mcc进行查阅。本文摘抄部分信息如下。

1

2

3

4

e Macro that generates a C Windows application on the Windows platform. On

non-Windows platforms, it is the same as the macro -m. This is

equivalent to the options "-W WinMain -T link:exe", which can be found

in the file <MATLAB>/toolbox/compiler/bundles/macro_option_e.

 

Matlab之GUI生成EXE文件