C + + read-write. Mat file Method _c language

Source: Internet
Author: User

In the process of writing a C + + program recently, you need to use the. mat file generated by Matlab, and then look for basic usage.

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

First, the project configuration:
1.vc++ directory-> include directory additions:
Matlab\r2010b\extern\include
Matlab\r2010b\extern\include\win64
-> Library Directory Add:
Matlab\r2010b\extern\lib\win64\microsoft
Matlab\r2010b\extern\lib\win32\microsoft
2.c/c++-> General-> Additional include directory add:
Matlab\r2010b\extern\include
Matlab\r2010b\extern\include\win64
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:

Copy Code code as follows:

#include <mat.h>

[CPP] View plaincopy

Matfile *pmatfile = NULL;
Mxarray *pmxarray = NULL;

Read. Mat file (example: Mat file name "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 files
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");
Mxsetdata (Pmxarray, Outa);
Matputvariable (Pmatfile, "A", Pmxarray);
Matclose (Pmatfile);


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. Use the Mxsetdata function to save a variable to a data array

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


6. 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


7. 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.

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.