C + + MATLAB interface __c++

Source: Internet
Author: User

And the original process is somewhat different, according to the specific circumstances of their own configuration can be.

Transferred from: http://blog.csdn.net/left_la/article/details/8206645

My computer environment is Win7 64-bit system, Vs2010,matlab r2010b.

First, the project configuration:
1.c/c++-> General-> Additional include directory add:
Matlab\r2010b\extern\include
Matlab\r2010b\extern\include\win64
2. Linker-> General-> Additional Library directory add:
Matlab\r2010b\extern\lib\win64\microsoft
Matlab\r2010b\extern\lib\win32\microsoft
3. Linker-> input-> additional dependent libraries add:
Libmat.lib
Libmx.lib
Libmex.lib
Libeng.lib

Since the installed MATLAB is 64 bits, to call its function, the project needs to be converted to X64
4. Top menu-> Generate-> Configuration Manager-> Platform: X64
5. Linker-> Advanced-> target computer:
MachineX64 (/machine:x64)

6. Computer environment variable->path add:
E:\DevTools\MATLAB\R2010b\extern\lib\win64\microsoft;
E:\DevTools\MATLAB\R2010b\bin\win64;

Second, the use of the basic read write Mat file code example:

First you need to include the header file:

#include <mat.h>

Matfile *pmatfile = NULL;  
Mxarray *pmxarray = NULL;  
  
Read the. Mat file (example: Mat file named "Initurban.mat", which contains "Inita")  
double *inita;  
  
Pmatfile = Matopen ("Initurban.mat", "R");  
Pmxarray = matgetvariable (Pmatfile, "Inita");  
Inita = (double*) mxgetdata (Pmxarray);  
M = Mxgetm (Pmxarray);  
N = MXGETN (Pmxarray);  
Matrix<double> A (m,n);  
for (int i=0; i<m; i++) for  
    (int j=0; j<n; j + +)  
        a[i][j] = inita[m*j+i];  
  
Matclose (pmatfile);  
Mxfree (Inita);  
  
Generate. mat file  
Double *outa = new Double[m*n];  
    for (int i=0; i<m; i++) for  
        (int j=0; j<n; j + +)  
            outa[m*j+i] = a[i][j];  
Pmatfile = Matopen ("A.mat", "w");  
Pmxarray = Mxcreatedoublematrix (M, N, mxreal);  
Mxsetdata (Pmxarray, outa);  
Matputvariable (Pmatfile, "A", Pmxarray);  


Iii. Description of the procedure

1. Use the Matopen function to open the mat file

Matfile *matopen (const char *filename,const char *mode)

Mode
R: Open in read-only mode
U: Update mode, readable and writable, but no new file will be created if the data file you want to open does not exist
W: Open as write, can only be written as in, create a new file if the data file you want to open does not exist


2. Using the Matgetvariable function to read a variable in a mat file

Mxarray * matgetvariable (Matfile * pMF, const char * name);

Read the variable named name and return a data array pointer


3. Use the Mxgetdata function to get data from a data array

void *mxgetdata (const mxarray *PA);

You need to use coercion type conversion when returning.


4. Using the Mxgetm and MXGETN functions to get the dimensions of the data array matrix

size_t Mxgetm (const mxarray *PA);

size_t mxgetn (const mxarray *PA);


5. Using the Mxcreatedoublematrix function to create a data array as a double floating-point number matrix

Mxarray *mxcreatedoublematrix (mwsize m, mwsize N, mxcomplexity flag);


6. Use the Mxsetdata function to save a variable to a data array

void Mxsetdata (Mxarray *pa, void *newdata);


7. Use the Matputvariable function to deposit the data array into the mat file

int matputvariable (Matfile * pMF, const char * name, const mxarray * PA);

Deposit successfully return 0, save error return non 0


8. Since the storage of matrices in Malab is stored in columns and is different from C, matrices obtained from mat files need to be rearranged. When generating mat files, be aware of the same.

9. After completing the mat file operation, you need to call the Matclose function to close the file and use Mxfree to free the memory

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.