core/matlab/examples/refbook/xtimesy.c

00001 #include "mex.h"
00002 
00003 /*
00004  * xtimesy.c - example found in API guide
00005  *
00006  * multiplies an input scalar times an input matrix and outputs a
00007  * matrix 
00008  *
00009  * This is a MEX-file for MATLAB.
00010  * Copyright 1984-2006 The MathWorks, Inc.
00011  */
00012 
00013 /* $Revision: 1.1 $ */
00014 
00015 void xtimesy(double x, double *y, double *z, mwSize m, mwSize n)
00016 {
00017   mwSize i,j,count=0;
00018   
00019   for (i=0; i<n; i++) {
00020     for (j=0; j<m; j++) {
00021       *(z+count) = x * *(y+count);
00022       count++;
00023     }
00024   }
00025 }
00026 
00027 /* the gateway function */
00028 void mexFunction( int nlhs, mxArray *plhs[],
00029                   int nrhs, const mxArray *prhs[])
00030 {
00031   double *y,*z;
00032   double  x;
00033   mwSize mrows,ncols;
00034   
00035   /*  check for proper number of arguments */
00036   /* NOTE: You do not need an else statement when using mexErrMsgTxt
00037      within an if statement, because it will never get to the else
00038      statement if mexErrMsgTxt is executed. (mexErrMsgTxt breaks you out of
00039      the MEX-file) */
00040   if(nrhs!=2) 
00041     mexErrMsgTxt("Two inputs required.");
00042   if(nlhs!=1) 
00043     mexErrMsgTxt("One output required.");
00044   
00045   /* check to make sure the first input argument is a scalar */
00046   if( !mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) ||
00047       mxGetN(prhs[0])*mxGetM(prhs[0])!=1 ) {
00048     mexErrMsgTxt("Input x must be a scalar.");
00049   }
00050   
00051   /*  get the scalar input x */
00052   x = mxGetScalar(prhs[0]);
00053   
00054   /*  create a pointer to the input matrix y */
00055   y = mxGetPr(prhs[1]);
00056   
00057   /*  get the dimensions of the matrix input y */
00058   mrows = mxGetM(prhs[1]);
00059   ncols = mxGetN(prhs[1]);
00060   
00061   /*  set the output pointer to the output matrix */
00062   plhs[0] = mxCreateDoubleMatrix(mrows,ncols, mxREAL);
00063   
00064   /*  create a C pointer to a copy of the output matrix */
00065   z = mxGetPr(plhs[0]);
00066   
00067   /*  call the C subroutine */
00068   xtimesy(x,y,z,mrows,ncols);
00069   
00070 }

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