MATLAB and C + + use. mat file to flow data to each other

Source: Internet
Author: User

Years ago, before the holiday home, the use of the data between C + + and MATLAB Interaction of an experiment, feeling very good effect. Preliminary to achieve the purpose, so tidy down to facilitate the use of everyone. Reduce the time you learn to program.
Hope to be of use to you.

#include "Mat.h"

void Example8 () {const char **fnames; /* Pointers to field names */const mwsize *dims; Mxarray *tmp, *fout; Char *pdata=null; int I_field, nfields; Mxclassid *classidflags; Mwindex j_struct; Mwsize numstructrecords; Mwsize Ndim; Matfile *pmf;mxarray *proad, *pout;int status;const char *file = "Contourroadresults_afterwidthetccorrect_4_zy.mat"; printf ("Opening file%s...\n\n", file);p MF = matopen (File, "U");p road=matgetvariable (PMF, "contourroadresults"); /* Get input arguments */nfields = Mxgetnumberoffields (proad); Numstructrecords = Mxgetnumberofelements (proad); /* Allocate memory for storing classidflags */classidflags = (mxclassid*) mxcalloc (nfields, sizeof (MXCLASSID)); /* Check empty field, proper data type, and data type consistency; * and get ClassID for each field. */For (i_field=0, i_field<nfields; i_field++) {for (j_struct = 0; j_struct < numstructrecords; j_struct++) {tmp = Mxgetfieldbynumber (Proad, j_struct, I_field); if (tmp = = NULL) {printf ("%s%d\t%s%d\n", "FIELD:", i_field+1, "STRUCT INDEX:", j_struct+1); Mexerrmsgidandtxt ("MATLAB:phonebook:fieldEmpty", "Above field is empty!"); } if (j_struct==0) {if (!mxischar (TMP) &&!mxisnumeric (tmp)) | | mxissparse (TMP)) { printf ("%s%d\t%s%d\n", "FIELD:", i_field+1, "STRUCT INDEX:", j_struct+1); Mexerrmsgidandtxt ("MATLAB:phonebook:invalidField",//"Above field must has either string O R numeric non-sparse data. "); CLASSIDFLAGS[I_FIELD]=MXGETCLASSID (TMP); } else {if (MXGETCLASSID (TMP) = Classidflags[i_field]) {printf ("%s%d\t%s%d\n", "field : ", I_field+1," STRUCT INDEX: ", j_struct+1); Mexerrmsgidandtxt ("MATLAB:phonebook:invalidFieldTypE ",//" inconsistent data type in above field! "); } else if (!mxischar (TMP) && ((Mxiscomplex (tmp) | | mxgetnumberofelements (TMP)!=1)) { printf ("%s%d\t%s%d\n", "FIELD:", i_field+1, "STRUCT INDEX:", j_struct+1); Mexerrmsgidandtxt ("MATLAB:phonebook:fieldNotRealScalar",//"Numeric data in above field must Be scalar and noncomplex! ");}}} /* Allocate memory for storing pointers */fnames = (const char * *) mxcalloc (nfields, sizeof (*fnames)); /* Get field name pointers */for (i_field=0; i_field< nfields; i_field++) {Fnames[i_field] = (char*) mxgetfie Ldnamebynumber (Proad,i_field); }/* Create a 1x1 struct matrix for output */POut = Mxcreatestructmatrix (1,1,nfields, fnames); Mxfree ((void *) fnames); Ndim = Mxgetnumberofdimensions (proad); dims = Mxgetdimensions (proad); FoR (i_field=0; i_field<nfields; i_field++) {/* Create cell/numeric Array */if (Classidflags[i_field] = = mx Char_class) {fout = Mxcreatecellarray (Ndim, dims); }else {fout = Mxcreatenumericarray (Ndim, dims, Classidflags[i_field], mxreal); pdata = (char*) mxgetdata (fout); }/* Copy data from input structure array */for (j_struct=0; j_struct<numstructrecords; j_struct++) { TMP = Mxgetfieldbynumber (Proad,j_struct,i_field); if (Mxischar (TMP)) {Mxsetcell (Fout, J_struct, Mxduplicatearray (TMP)); }else {mwsize sizebuf; SIZEBUF = Mxgetelementsize (TMP); memcpy (pdata, Mxgetdata (TMP), SIZEBUF); pdata + = Sizebuf; }}/* Set each field in output structure */Mxsetfieldbynumber (pOut, 0, I_field, fout); }matputvariable (PMF, "Outputresult_convert", POut); Mxfree (Classidflags);if (Matclose (pMF)! = 0) {printf ("Error closing file%s\n", file); return;} printf ("done\n"); return;}

The project requires a standard configuration of the prior C + + MEX hybrid programming, which is no longer mentioned. To be reminded, the header file that needs to be added is # include "Mat.h". In addition, the specific other functions, please refer to the MATLAB mat file read and write related content.

This help is more detailed in the description of the relevant function, you can quickly get started by referencing the relevant demo file.

wish to solve your problem!

MATLAB and C + + use. mat file to flow data to each other

Related Article

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.