core/matlab/examples/mex/mexgetarray.c

00001 /*=================================================================
00002  * mexgetarray.c 
00003  *
00004  * This example demonstrates how to use mexGetArray, mexPutArray, and
00005  * mexFunctionName. This function takes no input arguments. It counts
00006  * the number of times mexgetarray.c is called.  It has an internal
00007  * counter in the MEX-file and a counter in the MATLAB global
00008  * workspace.  Even if the MEX-file is cleared, it will not lose count
00009  * of the number of times the MEX-file has been called.
00010  *
00011  * This is a MEX-file for MATLAB.  
00012  * Copyright 1984-2006 The MathWorks, Inc.
00013  * All rights reserved.
00014  *=================================================================*/
00015  
00016 /* $Revision: 1.1 $ */
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;      /* unused parameters */
00033     (void) prhs;
00034 
00035     /* Check for proper number of input and output arguments */    
00036     if (nrhs !=0) {
00037         mexErrMsgTxt("No input arguments required.");
00038     } 
00039     if(nlhs > 1){
00040         mexErrMsgTxt("Too many output arguments.");
00041     }
00042     
00043     /* Make copy of MEX-file name, then create variable for MATLAB
00044        workspace from MEX-file name. */
00045     strcpy(array_name, mexFunctionName());
00046     strcat(array_name,"_called");
00047     
00048     /* Get variable that keeps count of how many times MEX-file has
00049        been called from MATLAB global workspace. */
00050     array_ptr = mexGetVariable("global", array_name);
00051     
00052     /* Check status of MATLAB and MEX-file MEX-file counter */    
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     /* Since variable does not yet exist in MATLAB workspace,
00063            create it and place it in the global workspace. */
00064     array_ptr=mxCreateDoubleMatrix(1,1,mxREAL);
00065     }
00066     
00067     /* Increment both MATLAB and MEX counters by 1 */
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     /* Put variable in MATLAB global workspace */
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     /* Destroy array */
00081     mxDestroyArray(array_ptr);
00082 }
00083 
00084 

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy