00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <stdio.h>
00021 #include <string.h>
00022 #include "mex.h"
00023
00024 static FILE *fp=NULL;
00025
00026
00027
00028
00029 static void CloseStream(void)
00030 {
00031 mexPrintf("Closing file matlab.data.\n");
00032 fclose(fp);
00033 }
00034
00035 void
00036 mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
00037 {
00038 char *str;
00039 (void) plhs;
00040
00041
00042 if (nrhs != 1) {
00043 mexErrMsgTxt("One input argument required.");
00044 }
00045 if(nlhs > 1){
00046 mexErrMsgTxt("Too many output arguments.");
00047 }
00048 if (!(mxIsChar(prhs[0]))){
00049 mexErrMsgTxt("Input must be of type string.\n.");
00050 }
00051
00052 if (fp==NULL){
00053 fp = fopen("matlab.data", "w");
00054 if (fp == NULL){
00055 mexErrMsgTxt("Could not open file matlab.data.");
00056 }
00057 mexPrintf("Opening file matlab.data.\n");
00058
00059
00060
00061 mexAtExit(CloseStream);
00062 }
00063
00064
00065 str=mxArrayToString(prhs[0]);
00066 if ((size_t)fprintf(fp,"%s\n", str) != strlen(str) +1){
00067 mxFree(str);
00068 mexErrMsgTxt("Could not write data to file.\n");
00069 }
00070 mexPrintf("Writing data to file.\n");
00071 mxFree(str);
00072 }