00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <stdio.h>
00019 #include <string.h>
00020 #include "mex.h"
00021
00022 static int mex_count = 0;
00023
00024 void
00025 mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
00026 {
00027
00028 char array_name[40];
00029 mxArray *array_ptr;
00030 int status;
00031
00032 (void) plhs;
00033 (void) prhs;
00034
00035
00036 if (nrhs !=0) {
00037 mexErrMsgTxt("No input arguments required.");
00038 }
00039 if(nlhs > 1){
00040 mexErrMsgTxt("Too many output arguments.");
00041 }
00042
00043
00044
00045 strcpy(array_name, mexFunctionName());
00046 strcat(array_name,"_called");
00047
00048
00049
00050 array_ptr = mexGetVariable("global", array_name);
00051
00052
00053
00054 if (array_ptr == NULL ){
00055 if( mex_count != 0){
00056 mex_count = 0;
00057 mexPrintf("Variable %s\n", array_name);
00058 mexErrMsgTxt("Global variable was cleared from the MATLAB \
00059 global workspace.\nResetting count.\n");
00060 }
00061
00062
00063
00064 array_ptr=mxCreateDoubleMatrix(1,1,mxREAL);
00065 }
00066
00067
00068 mxGetPr(array_ptr)[0]+=1;
00069 mex_count=(int)mxGetPr(array_ptr)[0];
00070 mexPrintf("%s has been called %i time(s)\n", mexFunctionName(), mex_count);
00071
00072
00073 status=mexPutVariable("global", array_name, array_ptr);
00074
00075 if (status==1){
00076 mexPrintf("Variable %s\n", array_name);
00077 mexErrMsgTxt("Could not put variable in global workspace.\n");
00078 }
00079
00080
00081 mxDestroyArray(array_ptr);
00082 }
00083
00084