matlab与C++以.mat文件方式进行数据相互流动

 

年前,放假回家之前,使用了C++与matlab之间的数据的互动的一个实验,感觉效果挺好。初步达到了目的,所以整理下来方便大家使用。减少大家编程学习的时间。
希望对你们有用。

#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); pMF = matOpen(file, "u"); pRoad=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 have either string or 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*)mxGetFieldNameByNumber(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] == mxCHAR_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; }

 项目需要进行事先的C++ MEX混合编程的标准配置,再此不再赘述。要提醒的是,需要加入的头文件是#include "mat.h"。另外,具体的其他函数,请参照matlab的 MAT文件的读写的相关内容。

这个帮助对相关函数说明比较详细,参照相关的demo文件,你就能很快上手。

祝愿能解决你的问题!

 

posted @ 2016-02-27 21:51  wenglabs  阅读(1257)  评论(0编辑  收藏  举报