我是靠谱客的博主 任性热狗,这篇文章主要介绍vc调用matlab的m文件混合编程(引擎方式),现在分享给大家,希望可以做个参考。

// test.cpp : 定义控制台应用程序的入口点。

#include "stdafx.h"
#include<vector>
#include<iostream>
#include<fstream>
/****************************************/

#include<Windows.h>
/*********************************
引擎调用
*******************************/
#include"engine.h"


// link matlab sys lib
#pragma comment(lib,"libeng.lib")
#pragma comment(lib,"libmx.lib")
#pragma comment(lib,"libmat.lib")
using namespace std;
bool readFile(vector<double> &vec_in, ifstream &infile)
{
    double temp;
    while(infile>>temp)
        vec_in.push_back(temp);
    return true;
}
bool ellipsefit_engine(Engine *ep,double &Xc,double &Yc,double &A,double &B,double &Phi,double &P,vector<double> x,vector<double> y)
{
    mxArray *mxa_x, *mxa_y;
    mxArray *mxa_Xc = NULL,*mxa_Yc = NULL,*mxa_A = NULL,*mxa_B = NULL,*mxa_Phi = NULL,*mxa_P = NULL;

    mxa_x = mxCreateDoubleMatrix(x.size(),1 , mxREAL);
    mxa_y = mxCreateDoubleMatrix(y.size(),1 , mxREAL);


    memcpy(mxGetPr(mxa_x),&x[0], y.size()*sizeof(double));
    memcpy(mxGetPr(mxa_y),&y[0], y.size()*sizeof(double));//OK ,复制数据到mx矩阵

    //memcpy(mxGetPr(mxa_x),&x.at(0), y.size()*sizeof(double));
    //memcpy(mxGetPr(mxa_y),&y.at(0), y.size()*sizeof(double));//OK
    //memcpy(mxGetPr(mxa_x),(void*)&(*x.begin()), y.size()*sizeof(double));
    //memcpy(mxGetPr(mxa_y),(void*)&(*y.begin()), y.size()*sizeof(double));//OK

    //copy(x.begin(),x.end(),mxGetPr(mxa_x));
    //copy(y.begin(),y.end(),mxGetPr(mxa_y));//OK
    //mxSetPr(mxa_x,&x[0]);
    //mxSetPr(mxa_y,&y[0]);//OK,但是不能释放内存空间

    engPutVariable(ep,"x",mxa_x);
    engPutVariable(ep,"y",mxa_y);

    mxDestroyArray(mxa_x);
    mxDestroyArray(mxa_y);
    engEvalString(ep,"userpath('C:UsersAdministratorDesktopeclipse_ctest');");//更改matlab工作空间,使其指向调用的m文件的地址
    engEvalString(ep,"[Xc,Yc,A,B,Phi,P]=ellipsefit(x,y);");//前边一定要更改空间,使其指向函数[Xc,Yc,A,B,Phi,P]=ellipsefit(x,y)所在的m文件
    mxa_Xc=engGetVariable(ep,"Xc");
    mxa_Yc=engGetVariable(ep,"Yc");
    mxa_A=engGetVariable(ep,"A");
    mxa_B=engGetVariable(ep,"B");
    mxa_Phi=engGetVariable(ep,"Phi");
    mxa_P=engGetVariable(ep,"P");
    double *p_Xc = mxGetPr(mxa_Xc);// 将 matlab 中的矩阵的指针传递给 C 语言中的指向 double 的指针
    Xc=p_Xc[0];

    double *p_Yc =mxGetPr(mxa_Yc);
    Yc = p_Yc[0];

    double *p_A =mxGetPr(mxa_A);
    A = p_A[0];

    double *p_B =mxGetPr(mxa_B);
    B = p_B[0];

    double *p_Phi =mxGetPr(mxa_Phi);
    Phi = p_Phi[0];

    double *p_P =mxGetPr(mxa_P);
    P = p_P[0];

    return true;
}
int main(int argc, _TCHAR* argv[])
{
    Engine* pEng=NULL;
    if(!(pEng=engOpen(NULL)))
    {
        printf("Open matlab engine fail!n");
        return 0;
    }
    else
       printf("Open Engine Sucess!n");
    engSetVisible(pEng,0);//隐藏matlab窗口,1显示窗口
    cout << "开始运行" << endl;
    ifstream infile_x,infile_y;
    char *infile_name_x="x.txt";
    char *infile_name_y="y.txt";
    infile_x.open(infile_name_x);
    vector<double> vec_x;
    readFile(vec_x,infile_x);
    infile_y.open(infile_name_y);
    vector<double> vec_y;
    readFile(vec_y,infile_y);
    double Xc,Yc,A,B,Phi,P; 
    ellipsefit_engine(pEng,Xc,Yc,A,B,Phi,P,vec_x,vec_y);
    cout<<"Xc="<<Xc<<endl;
    cout<<"Yc="<<Yc<<endl;
    cout<<"A="<<A<<endl;
    cout<<"B="<<B<<endl;
    cout<<"Phi="<<Phi<<endl;
    cout<<"P="<<P<<endl;

    printf("Engine will be closedn");
    system("pause");
    if(NULL!=pEng)
        engClose(pEng);
    printf("Engine is closed");
    return 0;
}

最后

以上就是任性热狗最近收集整理的关于vc调用matlab的m文件混合编程(引擎方式)的全部内容,更多相关vc调用matlab内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(174)

评论列表共有 0 条评论

立即
投稿
返回
顶部