Matlab代码转C++(一)

    xiaoxiao2021-04-16  27

        所用matlab版本为:Matlab 2013a 32位  所用VS版本为: VS2010  32位。 如果用64位matlab进行转化,VS要用64位编辑器,否则,编辑时会出现”***无法解析***“的问题,因此建议用32的matlab进行转化。

        转化前的前期准备,见链接: http://blog.sina.com.cn/s/blog_67dd3b950101es47.html

    一、转化成exe文件:

    1.安装 MCRInstaller.exe

     MCRInstaller.exe 在安装目录 **\MATLAB\R2013a\toolbox\compiler\deploy\win32下,点击直接安装即可。 2.切换命令窗口地址 把matlab命令窗口地址,切换到要转化的.m文件所在文件夹地址,这样转化生成的exe会和.m文件在同一文件夹中。 3.执行转化命令 在matlab的命令窗口中输入:mcc -m main ,即开始转化。(main为待转化的.m文件名) 如果提示如下错误,就把mcc -m main 手动输入一下:

    到此,exe文件就生成了。

    二、转化成.dll、.lib、.h、.cpp文件

    1.新建一个test.m文件,并保存:(以相加为例)

    [cpp] view plain copy print ? function [C] = test(A,B)  C = A+B;  end   function [C] = test(A,B) C = A+B; end 2.切换命令窗口地址:

    命令窗口地址切换到test.m所在文件夹地址。

    3.执行转化命令:

    mcc -W cpplib:Mytest -T link:lib test.m

    参数说明:  -W:       控制编译后的封装格式

                           cpplib:表示生成C++的lib,会生成.cpp文件  去掉cpp,会生成C的lib,生成.c文件;冒号之后,是生成的.dll、.lib、.h、.cpp的文件名

                           link:lib:表示连接到lib的目标

                           test.m :  待转化的.m文件名

    到此,即生成了.dll、.lib、.h、.cpp文件以及其他文件。

    三、VS中调用test函数

    在VS中调用test函数,只需要生成的.dll、.lib、.h文件,具体过程如下:

    1.配置VS

    <1>把matlab安装目录 ***\MATLAB\R2013a\extern 下的include文件夹,拷贝到工程目录下

    同时,在工程目录下新建lib文件夹,并把***\MATLAB\R2013a\extern\lib\win32\microsoft 下的所有lib文件,拷贝到所建lib文件夹中。

    * 把生成的.h文件,拷贝到include文件夹中,把.lib文件拷贝到lib文件夹中。

    <2>在工程属性页的  配置属性->VC++ 目录->包含目录 中,添加include文件夹路径

           在工程属性页的  配置属性->VC++目录->库目录 中,添加lib文件夹路径

           在工程属性页的  配置属性->连接器->输入 中输入 mclmcrrt.lib 和 Mytest.lib。(mclmcrrt.lib为matlab自身的lib文件,Mytest.lib为matlab生成的lib文件)

    <3>把生成的.dll文件拷贝到,工程目录的Debug文件夹中。(新建VS工程,编辑后,会生成Debug文件夹)

    2.编写程序调用test函数

    [cpp] view plain copy print ? #include "stdafx.h"  #include "engine.h"  #include "Mytest.h"    using namespace std;  int main()  {      int a = 4;      int b = 3;      int c;      if(!MytestInitialize())  //初始化库文件,在.h文件中有定义,Mytest为生成文件名      {             cout<<lib Mytest Initialize failed!\n<<endl;           return 0;        }          mwArray mwa(1,1,mxUINT32_CLASS);  //给输入参数A赋值      mwa.SetData(&a,1);         mwArray mwb(1,1,mxUINT32_CLASS);  //给输入参数B赋值      mwb.SetData(&b,1);          mwArray mwy(1,1,mxUINT32_CLASS);             test(1,mwy,mwa,mwb);  //调用test函数,该函数在.h中有定义                            //第一个参数:.m文件中test函数对应的输出参数个数                            //第二个参数:.m文件中test函数对应的输出参数C                            //第三个参数:.m文件中test函数对应的输入参数A                            //第四个参数:.m文件中test函数对应的输入参数B      mwy.GetData(&c,1);  //获取test计算结果C            cout<<c<<endl;      MytestTerminate(); //结束end 在.h中有定义    }   #include "stdafx.h" #include "engine.h" #include "Mytest.h" using namespace std; int main() { int a = 4; int b = 3; int c; if(!MytestInitialize()) //初始化库文件,在.h文件中有定义,Mytest为生成文件名 { cout<<lib Mytest Initialize failed!\n<<endl; return 0; } mwArray mwa(1,1,mxUINT32_CLASS); //给输入参数A赋值 mwa.SetData(&a,1); mwArray mwb(1,1,mxUINT32_CLASS); //给输入参数B赋值 mwb.SetData(&b,1); mwArray mwy(1,1,mxUINT32_CLASS); test(1,mwy,mwa,mwb); //调用test函数,该函数在.h中有定义 //第一个参数:.m文件中test函数对应的输出参数个数 //第二个参数:.m文件中test函数对应的输出参数C //第三个参数:.m文件中test函数对应的输入参数A //第四个参数:.m文件中test函数对应的输入参数B mwy.GetData(&c,1); //获取test计算结果C cout<<c<<endl; MytestTerminate(); //结束end 在.h中有定义 }

    注:      1.mwArray是一种C++和matlab结合的数据类型,生成的函数参数定义类型为mwArray型,所以在VS中需要用mwArray参数,来调用test函数,生成的test函数定义如下:

          

    [cpp] view plain copy print ? extern LIB_Mytest_CPP_API void MW_CALL_CONV test(int nargout, mwArray& C, const mwArray& A, const mwArray& B);   extern LIB_Mytest_CPP_API void MW_CALL_CONV test(int nargout, mwArray& C, const mwArray& A, const mwArray& B);

        2.VS建工程时,选用了MFC头文件,所以添加了stdafx.h头文件,如果没有用MFC头文件,需去掉该头文件。

                            

                        

    转载请注明原文地址: https://ju.6miu.com/read-672081.html

    最新回复(0)