core/matlab/examples/mx/mxgetinf.c

00001 /*=================================================================
00002  * mxgetinf.c 
00003  *
00004  * mxgetinf takes one input argument of type double floating
00005  * integers.  For this example, zero values are used to indicate missing
00006  * data.  It replaces all zeros with NaN.  Values greater than equal to
00007  * INT_MAX are replaced with infinity.  Values less than or equal to
00008  * INT_MIN are replaced with minus infinity.  mxgetinf returns the data
00009  * with the replaced values.
00010  *
00011  * This is a MEX-file for MATLAB.  
00012  * Copyright 1984-2006 The MathWorks, Inc.
00013  * All rights reserved.
00014  *=================================================================*/
00015 /* $Revision: 1.1 $ */
00016 
00017 #include <limits.h>
00018 #include "mex.h"
00019 
00020 void 
00021 mexFunction( int nlhs, mxArray *plhs[],  int nrhs, const mxArray *prhs[])
00022 {
00023     mwSize i, n;
00024     double *pr, *pi;
00025     double inf, nan;
00026     
00027     /* Check for proper number of input and output arguments */    
00028     if (nrhs != 1) {
00029     mexErrMsgTxt("One input argument required.");
00030     } 
00031     if(nlhs > 1){
00032     mexErrMsgTxt("Too many output arguments.");
00033     }
00034     
00035     /* Check data type of input argument  */
00036     if (!mxIsDouble(prhs[0]) || mxIsComplex(prhs[0])) {
00037     mexErrMsgTxt("Input argument must be of type real double.");
00038     }   
00039     
00040     /* Duplicate input array */
00041     plhs[0]=mxDuplicateArray(prhs[0]);
00042     
00043     pr = mxGetPr(plhs[0]);
00044     pi = mxGetPi(plhs[0]);
00045     n = mxGetNumberOfElements(plhs[0]);
00046     inf = mxGetInf();
00047     nan=mxGetNaN();
00048 
00049     /* Check for 0, in real part of data, if the data is zero, replace
00050        with NaN.  Also check for INT_MAX and INT_MIN and replace with
00051        INF/-INF respectively.  */
00052     for(i=0; i < n; i++) {
00053     if (pr[i] == 0){
00054         pr[i]=nan;
00055     }
00056     else if ( pr[i]>= INT_MAX){
00057         pr[i]=inf;
00058     }
00059     else if (pr[i]<= INT_MIN){
00060         pr[i]=-(inf);
00061     }
00062     }
00063 }
00064 
00065 
00066 
00067 
00068 

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