Proficient VC and MATLAB joint programming (eight)

Source: Internet
Author: User
Tags response code

In the previous chapter of the MATLAB compiler did a brief introduction, and introduced how to convert m file into VC callable DLL file, in this chapter describes how to use the compiler to convert m files to the corresponding c\c++ file, and in VC call. The examples in this chapter are the solutions to linear equations that are introduced in the fourth chapter.

First, make sure the compiler is set up correctly, and the compiler's settings refer to the previous two chapters. This step is not required if you have previously configured it.

1, in the MATLAB to write the following functions: function [x]=gjfcz(A,b)
%A=[-1.5 1 2; 4 2 3 ; -3 2 8]
%b=[3;5;6]
x=A\b
Save the name of GJFCZ.M, the function for solving linear equations, can refer to the fourth chapter of the content.

2, in the MATLAB command window to enter the following command:mcc –m gjfcz.m

This command is used to generate the corresponding C file and executable program. In the MATLAB working directory (generally is matlab\work) will generate the following file: Gjfcz.exe,gjfcz.c,gjfcz.h,gjfcz_main.c, where gjfcz.c,gjfcz.h is the file we need.

3, a new project called JXXFC based on the dialog box, add a button on the panel.

4, copy gjfcz.c,gjfcz.h two files to the engineering directory, and the document into the project (Project->add to Project->files).

5, add the following response code for the button:

void CJXXFCDlg::OnButton1()
{
  static  double Adata[]={-1.5,4,-3,1,2,2,2,3,8};
  static  double bdata[]={3,5,6};
  double  Xdata[100];
  mxArray *A = NULL;//赋初值
  mxArray *b = NULL;
  mxArray *x = NULL;
  /* 使用自动内存管理*/
  mlfEnterNewContext(0, 0);
  //创建矩阵
  mlfAssign(&A, mlfDoubleMatrix(3, 3, Adata, NULL));
  mlfAssign(&b, mlfDoubleMatrix(3, 1, bdata, NULL));
  InitializeModule_gjfcz();
  x=mlfGjfcz(A,b);//调用gjfcz.c中的函数求解
  TerminateModule_gjfcz();
  memcpy(Xdata,mxGetPr(x),3*sizeof(double));
  // mxGetPr(x)用来得到x的地址
  CString R;
  R.Format("%f\n%f\n%f",Xdata[0],Xdata[1],Xdata[2]);
  MessageBox(R);
  /* 释放矩阵所占的内存*/
  mxDestroyArray(A);
  mxDestroyArray(b);
  mxDestroyArray(x);
  /* 禁用自动内存管理*/
  mlfRestorePreviousContext(0, 0);
}

Compile, connect, run results as shown in Figure 1:

Figure 1

This program in the vc6.0+matlab6.5 environment through the mode, VC and MATLAB interface programming, using the compiler to compile m files into c\c++ files, and then in VC call the C\c++ file is the most commonly used method. In this example, although the use of functions in the C\c++ function library, but compared to the full use of c\c++ function writing program to a lot simpler, visible to be flexible with the compiler, the C\c++ function library has a certain understanding is very necessary, MATLAB provides a library of help documents Cmath_ Ref2b.pdf and cmath_ug2b.pdf are available for inspection when used. Again, many friends are already using the MATLAB7.0 version, the 7.0 version of the C\c++ interface technology has made great changes, not a general upgrade, is not compatible with the previous version. The new version provides a class library similar to VC, and it doesn't feel good.

This article supporting source code

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.