core/matlab/examples/mex/mexsettrapflag.c

00001 /*=================================================================
00002  * mexsettrapflag.c 
00003  *
00004  * This example demonstrates how to use mexSetTrapFlag. If you call
00005  * this function with a 0, the trapflag is set to zero.  When
00006  * mexCallMATLAB fails, control goes directly to MATLAB.  If you call
00007  * this function with a 1, the trapflag is set to one.  When
00008  * mexCallMATLAB fails, control stays in the MEX-file and the MEX-file
00009  * causes the file to error out.
00010  *
00011  * This is a MEX-file for MATLAB.  
00012  * Copyright 1984-2006 The MathWorks, Inc.
00013  * =================================================================*/
00014 
00015 /* $Revision: 1.1 $ */
00016 #include "mex.h"
00017 
00018 void
00019 mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
00020 {
00021     int   status;
00022     mxArray *error_msg[1];
00023     char *buf;
00024 
00025     (void) plhs;      /* unused parameter */
00026 
00027     /* Check for proper number of input and output arguments */    
00028     if (nrhs !=1 || !mxIsDouble(prhs[0]) ||
00029     mxGetN(prhs[0])*mxGetM(prhs[0]) != 1 || mxIsComplex(prhs[0])) {
00030     mexErrMsgTxt("Input argument must be a real scalar double.");
00031     }
00032  
00033     if(nlhs > 1){
00034     mexErrMsgTxt("Too many output arguments.");
00035     }
00036 
00037     /* Get the input argument and use it to set mexTrapFlag */
00038     mexSetTrapFlag((int)mxGetScalar(prhs[0]));
00039     
00040     /* Invoke a function that does not exist */ 
00041     status = mexCallMATLAB(0, (mxArray **)NULL, 
00042                0, (mxArray **)NULL, "nofcn");
00043 
00044     /* If input argument to mexsettrapflag is non-zero, control
00045        remains in MEX-file. */
00046     
00047     if(status==0){
00048       mexPrintf("???? nofcn exists ????\n");
00049     }
00050     else{
00051 
00052     /* Display contents of MATLAB function lasterr to see error
00053        issued in MATLAB */
00054    
00055     mexCallMATLAB(1,error_msg,0, (mxArray **)NULL, "lasterr"); 
00056     buf = mxArrayToString(error_msg[0]);
00057     mexPrintf("The last error message issued in MATLAB was: \n%s\n", buf);
00058     mxFree(buf);    
00059     mexErrMsgTxt("mexCallMATLAB failed.\n");
00060     }
00061 }
00062     
00063     
00064     
00065 
00066 
00067      
00068 

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