linuxmatlab编译
1. MATLAB 2017 安装步骤(linux)
MATLAB 是一款用于算法开发、数据可视化、数据分析以及数值计算的高级技术计算语言和交互式环境的商业数学软件,由美国 MathWorks 公司出品。
安装 MATLAB 2017 在 CentOS7 环境的步骤如下:
1、首先,将 ISO 文件挂载到 /mnt 目录。使用命令:
#mount R2017b_glnxa64.iso /mnt -o loop
2、接着,运行以下命令启动安装交互界面:
#./install -javadir ./sys/java/jre/glnxa64/jre/
安装过程中,会弹出图形界面,请根据提示进行操作。
3、选择“Use a File installation Key”,然后输入购买的软件序列号,例如“12345-12345-12345-12345-12345”,并点击下一步。
4、在第六步中,设置安装路径。默认路径即可,或手动输入。
5、可以依据自身需求选择是否安装额外模块。如果不需要使用多核计算,可以不选择“MATLAB Distributed Computing Server 6.7”。
6、点击“Install”开始安装。整个安装过程根据机器配置的不同,可能需要几十分钟到几个小时。
7、安装完成后,不要立即启动 MATLAB,等待激活过程完成。
8、激活后,在证书所在目录下复制证书到目标目录。
9、设置环境变量。为了方便调用 MATLAB 及相关命令,将 MATLAB 安装目录的 bin 文件夹添加到 PATH 环境变量中。在用户目录下输入 Vi .bashrc,并添加以下命令:
export PATH=/usr/local/MATLAB/R2017b/bin:$PATH
10、刷新环境变量,运行 source ~/.bashrc。
11、最后,运行 matlab 命令,即可启动 MATLAB 的图形界面,开始正常使用这款软件。
2. Matlab安装教程(Linux)
在虚拟机环境中,通常会将安装包放置在/tmp/VMwareDnD目录下,需将文件移动至/home/。首先,使用以下命令进行文件转移:
然后,在包含安装包的目录中,执行解压缩操作。如果虚拟机中未预装解压命令,可通过以下命令安装:
在安装过程中,建议先阅读Readme文件,了解安装步骤。为了挂载iso文件进行安装,需要在相应位置创建挂载目录:
将iso文件挂载到挂载目录后,转到该目录并启动安装程序。若遇到安装选项,选择Advanced Options并选择File installation Key方式安装,键入在Readme中获取的Key(例如:05322-36228-06991-12654-51812-34369-14072-44298-22786-36732-05503-35033-50900-29808-05166-12170-05630-02560-02687-62114-45079-42917-06281-13007-19512-18270)。
接着,需要提供license file,通常在安装包内提供,输入对应目录。建议将license.lic保存在安装目录/opt/matlab/license/,便于后续使用。
为了安装在/opt/matlab目录下,需要先创建该目录。将license文件放置在相应位置,安装程序中输入路径为/opt/matlab/license/license.lic。
在安装步骤中,填写安装地址,根据需求选择安装内容。安装完成后,将补丁文件存放在指定位置,并卸载挂载的iso文件。
为了方便快捷启动Matlab,推荐将命令添加到PATH。首先,在/home/创建bin目录,然后创建启动命令脚本,确保有执行权限,接着在环境配置文件bashrc中添加环境变量设置,如需独立管理,可以在/home/下创建独立的环境文件并引入至bashrc中。
最后,在bashrc中输入配置命令,完成环境配置。现在,只需在命令行输入matlab,即可直接启动Matlab。
3. linux c++ 怎么 调用自己函数的
实验平台:ubuntu 12.04 + g++4.6 + matlab2012a
问题描述:
有一个c++程序main.cpp,和一个matlab函数myFunc.m。现在要做这件事:
1)从main.cpp中传递2个double类型的数值a和b到myFunc.m中
2)myFunc.m中求和(sum = a+b)
3)main.cpp中接收myFunc.m返回的和并输出。
思路:
1)设置matlab的编译器,使用gcc编译器。编译m文件成.so。
2)编译.cpp文件,编译时调用.so库(添加.so的路径到编译选项)。
步骤:
1)将自己的matlab函数(myFunc.m)编译成动态链接库
(1) 设定编译器为gcc,在matlab 命令行依次执行命令mex -setup和mbuild -setup:
>> mex -setup Options files control which compiler to use, the compiler and link command options, and the runtime libraries to link against. Using the 'mex -setup' command selects an options file that is placed in ~/.matlab/R2012a and used by default for 'mex'. An options file in the current working directory or specified on the command line overrides the default options file in ~/.matlab/R2012a. To override the default options file, use the 'mex -f' command (see 'mex -help' for more information).The options files available for mex are: 1: /opt/MATLAB/R2012a/bin/mexopts.sh : Template Options file for building gcc MEX-files 0: Exit with no changesEnter the number of the compiler (0-1):1/opt/MATLAB/R2012a/bin/mexopts.sh is being copied to ~/.matlab/R2012a/mexopts.shcp: cannot create regular file `~/.matlab/R2012a/mexopts.sh': Permission denied************************************************************************** Warning: The MATLAB C and Fortran API has changed to support MATLAB variables with more than 2^32-1 elements. In the near future you will be required to update your code to utilize the new API. You can find more information about this at: http://www.mathworks.com/help/techdoc/matlab_external/bsflnue-1.html Building with the -largeArrayDims option enables the new API. **************************************************************************>> mbuild -setup Options files control which compiler to use, the compiler and link command options, and the runtime libraries to link against. Using the 'mbuild -setup' command selects an options file that is placed in ~/.matlab/R2012a and used by default for 'mbuild'. An options file in the current working directory or specified on the command line overrides the default options file in ~/.matlab/R2012a. To override the default options file, use the 'mbuild -f' command (see 'mbuild -help' for more information).The options files available for mbuild are: 1: /opt/MATLAB/R2012a/bin/mbuildopts.sh : Build and link with MATLAB Compiler generated library via the system ANSI C/C++ compiler 0: Exit with no changesEnter the number of the compiler (0-1):1/opt/MATLAB/R2012a/bin/mbuildopts.sh is being copied to ~/.matlab/R2012a/mbuildopts.shcp: cannot create regular file `~/.matlab/R2012a/mbuildopts.sh': Permission denied>>
(2) 在matlab中,编写myFunc.m文件内容如下:
function [ C ] = myFunc(A, B)C = A+B;end
(3) 生成myFunc.m的动态链接库(.so)
>> mcc -W cpplib:libmyFunc -T link:lib myFunc.m -cWarning: MATLAB Toolbox Path Cache is out of date and is not being used.Type 'help toolbox_path_cache' for more info >>
等待数秒,完成。可以看到myFunc.m所在的目录下生成了多个文件:
$ lslibmyFunc.cpp libmyFunc.ctf libmyFunc.exports libmyFunc.h libmyFunc.so main.cpp mccExcludedFiles.log myFunc.m readme.txt
命
令解释:其中-W是控制编译之后的封装格式;cpplib是指编译成C++的lib;cpplib冒号后面是指编译的库的名字;-T表示目
标,link:lib表示要连接到一个库文件的目标,目标的名字即是.m函数的名字。-c表明需要生成.ctf文件,比如本例如果不加-c就不会生成
“libmyFunc.ctf”。
生成的文件中打开“libmyFunc.h”可以看到一行:
extern LIB_libmyFunc_CPP_API void MW_CALL_CONV myFunc(int nargout, mwArray& C, const mwArray& A, const mwArray& B);
这
个就是我们的myFunc.c函数待会儿在c++中调用时的接口。有4个参数,第一个是参数个数,第二个是用来接收函数返回值的,后面2个是从c++中传
递进来的变量。我们还会用到“libmyFunc.h”中的另外2个函数:libmyFuncInitialize()初始化,和注销
libmyFuncTerminate()。
2)编写main.cpp,代码如下:
#include <iostream>#include "mclmcr.h"#include "matrix.h"#include "mclcppclass.h"#include "libmyFunc.h"#include "mclmcrrt.h"using namespace std;int main() { // initialize lib,这里必须做初始化! if( !libmyFuncInitialize()) { std::cout << "Could not initialize libmyFunc!" << std::endl; return -1; } // 用户输入2个数值 double a, b; cout<<"Please input 2 numbers <a b> and then press enter: "<<endl; cin >> a; cin >> b; double c; //used to receive the result // 为变量分配内存空间, maltab只有一种变量,就是矩阵,为了和c++变量接轨,设置成1*1的矩阵 mwArray mwA(1, 1, mxDOUBLE_CLASS); //1,1表示矩阵的大小, mxDOUBLE_CLASS表示变量的精度 mwArray mwB(1, 1, mxDOUBLE_CLASS); mwArray mwC(1, 1, mxDOUBLE_CLASS); // 调用类里面的SetData函数给类赋值 mwA.SetData(&a, 1); mwB.SetData(&b, 1); // 调用自己的函数,求和。 myFunc(1, mwC, mwA, mwB); c = mwC.Get(1, 1); cout<<"The sum is: "<<c<<endl; // 后面是一些终止调用的程序 // terminate the lib libmyFuncTerminate(); // terminate MCR mclTerminateApplication(); return EXIT_SUCCESS;}
3)编译main.cpp函数,调用libmyFunc.so
$ g++ -o main -I. -I/opt/MATLAB/R2012a/extern/include -L. -L/opt/MATLAB/R2012a/runtime/glnxa64 main.cpp -lmyFunc -lm -lmwmclmcrrt -lmwmclmcr
开始编译时也遇到一些问题,主要是链接库路径问题导致的编译错误,详细错误汇总在另篇笔记里(点此查看)。
编译成功后,需要装一下MCR Installer,步骤点此。
运行结果:
$ ./main Warning: latest version of matlab app-defaults file not found.Contact your system administrator to have this file installedPlease input 2 numbers <a b> and then press enter: 10 100The sum is: 110$ ./main Warning: latest version of matlab app-defaults file not found.Contact your system administrator to have this file installedPlease input 2 numbers <a b> and then press enter: 1 1The sum is: 2
源代码及编译过程中的所有文件已打包,点击这里可以下载。