?? mexcreatematfile.c
字號:
/*necessory header file*/
#include "mat.h"
#include "math.h"
/*MEX function file entrance*/
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
mxArray *string,*scalar,*array;
double img[]={0,1,2,3,4,5,6,7,8};
double real[]={8,7,6,5,4,3,2,1,0};
double *pr,*pi;
mxChar *filename;
MATFile *file;
int buflen=0;
/*judge the input/output*/
if(nrhs!=1)
mexErrMsgTxt("an input is necessory to be the filename!");
if(mxGetClassID(prhs[0])!=mxCHAR_CLASS)
mexErrMsgTxt("input must be characters, stand for filename!");
/*create data*/
string=mxCreateString("MATLAB is wonderful!");
scalar=mxCreateDoubleScalar(2003);
array=mxCreateDoubleMatrix(3,3,mxCOMPLEX);
pr=mxGetPr(array);
pi=mxGetPi(array);
memcpy(pr,real,9*sizeof(double));
memcpy(pr,img,9*sizeof(double));
/*get the filename*/
buflen=mxGetNumberOfElements(prhs[0])+1;
filename=mxMalloc(buflen);
mxGetString(prhs[0],filename,buflen);
/*create new MAT file*/
file=matOpen(filename,"w");
if(file==NULL)
mexErrMsgTxt("can't create appointed file");
/*write data to MAT file*/
matPutVariable(file,"String",string);
matPutVariable(file,"Scalar",scalar);
matPutVariable(file,"Array",array);
/*close MAT file*/
matClose(file);
mexPrintf("successfully create MAT file:%s\n",filename);
/*release the memory*/
mxDestroyArray(string);
mxDestroyArray(scalar);
mxDestroyArray(array);
mxFree(filename);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -