年前,放假回家之前,使用了C++与matlab之间的数据的互动的一个实验,感觉效果挺好。初步达到了目的,所以整理下来方便大家使用。减少大家编程学习的时间。
希望对你们有用。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | #include "mat.h"<br data-filtered="filtered"><br data-filtered="filtered">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文件,你就能很快上手。
祝愿能解决你的问题!