⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mexcreatematfile.c

📁 c语言
💻 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 + -