VC call MATLAB DLL mode mixed programming (C-style-Example 2) < two >

Source: Internet
Author: User

VC and MATLAB mixed programming call dynamic link library dll--< two >
1, is the first to establish the M file matlab.
Ellipsefit.m
It contains: function [Xc,yc,a,b,phi,p]=ellipsefit (x, y)
2.MCC command compilation generated by H, DLL, lib and other files
In the MATLAB command line, enter:
Mcc-b csharedlib:ellipsefit ELLIPSEFIT.M (this mode is C-style function)
(The specific command meaning can be referred to "proficient in MATLAB and C + + mixed Programming" Liouville, 3rd edition, Beijing University of Aeronautics and Astronautics Press. )
(Ellipsefit is the generated LIB file name, which can be arbitrarily named.) ELLIPSEFIT.M is the. m file name that you want to invoke.
At this point, you can see that a series of files are generated in the directory where ELLIPSEFIT.M is located. We will use: Ellipsefit.h, Ellipsefit.dll, Ellipsefit.lib
2.3: Create a VC project: Test

For simplicity, we are building a Win32 console program here. As follows:
Then, put the above 3 files (ellipsefit.h, Ellipsefit.dll, Ellipsefit.lib
) to the directory in which the VC works, so that the VC can call it and add the. h file to the project.

2.3 VC Programming: (Code below)

//Test.cpp: Defines the entry point of the console application. //#include "stdafx.h"#include <vector>#include <iostream>#include <fstream>/********************************VC calls MCC-generated dll************************************/#include "mclmcrrt.h"#include "mclmcr.h"#include "mclcppclass.h"#include "matrix.h"//Include Lib head file#include "ellipsefit.h"//link matlab sys lib#pragma comment (lib, "Mclmcrrt.lib")#pragma comment (lib, "Libmx.lib")#pragma comment (lib, "Libmat.lib")#pragma comment (lib, "Mclmcr.lib")//Link lib#pragma comment (lib, "Ellipsefit.lib")using namespace STD;BOOLReadFile ( vector<double>&vec_in, Ifstream &infile) {DoubleTemp while(infile>>temp) Vec_in.push_back (temp);return true;}BOOLEllipsefit_dll (Double&AMP;XC,Double&AMP;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    //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, but cannot free memory spaceMlfellipsefit (6, &mxa_xc,&mxa_yc,&mxa_a,&mxa_b,&mxa_phi,&mxa_p, mxa_x, mxa_y);    Mxdestroyarray (mxa_x); Mxdestroyarray (mxa_y);Double*P_XC = MXGETPR (MXA_XC);//pass pointer to the matrix in MATLAB to a pointer to double in the C languagexc=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;}intMainintARGC, _tchar* argv[]) {if(!mclinitializeapplication (NULL,0))    {cout<<"Could not initialize the application."<< Endl;; }if(!ellipsefitinitialize ()) {cout<<"Can ' t initalize!"<< Endl; }cout<<"Start Running"<< 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);DoubleXc,yc,a,b,phi,p; Ellipsefit_dll (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;    Ellipsefitterminate ();    Mclterminateapplication (); System"Pause");return 0;}

Running Result: x64

VC call MATLAB DLL mode mixed programming (C-style-Example 2) < two >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.