📄 writemat.c
字号:
/* * writemat.c - Create a binary MAT file. * * Mastering MATLAB 7 C MAT-file Example 1 * */#include "mat.h"int makemat(const char *filename, double *data, int m, int n, char *mmstr) { MATFile *mfile; mxArray *mdata, *mstr; /* Open the MAT file for writing. */ mfile = matOpen(filename, "w"); if (mfile == NULL) { printf("Cannot open %s for writing.\n", filename); return(EXIT_FAILURE); } /* Create the mxArray to hold the numeric data. */ /* Note that the array dimensions are reversed. */ /* C uses row order while MATLAB uses column order. */ /* The data array will be transposed in MATLAB. */ mdata = mxCreateDoubleMatrix(n,m,mxREAL); /* Copy the data to the mxArray. Note that mxGetData is */ /* similar to mxGetPr in that it returns a void pointer */ /* while mxGetPr returns a pointer to a double. */ memcpy((void *)(mxGetData(mdata)), (void *)data, m*n*sizeof(double)); /* Create the string array. */ mstr = mxCreateString(mmstr); /* Write the mxArrays to the MAT file. */ matPutVariable(mfile, "mydata", mdata); matPutVariable(mfile, "mystr", mstr); /* Free the mxArray memory. */ mxDestroyArray(mdata); mxDestroyArray(mstr); /* Close the MAT file. */ if (matClose(mfile) != 0) { printf("Cannot close %s.\n",filename); return(EXIT_FAILURE); } return(EXIT_SUCCESS);}int main(){ int status; char *mmstr = "Mastering MATLAB Rocks!"; double data[3][4] = {{ 1.0, 2.0, 3.0, 4.0 }, { 5.5, 6.6, 7.7, 8.8 }, { -4.0, -3.0, -2.0, -1.0 }}; status = makemat("mmtest.mat", *data, 3, 4, mmstr); return(status);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -