core/matlab/examples/refbook/doubleelement.c

00001 #include <string.h> /* needed for memcpy() */
00002 #include "mex.h"
00003 
00004 /*
00005  * doubleelement.c - example found in API guide
00006  *
00007  * constructs a 2-by-2 matrix with unsigned 16-bit integers, doubles
00008  * each element, and returns the matrix
00009  *
00010  * This is a MEX-file for MATLAB.
00011  * Copyright 1984-2006 The MathWorks, Inc.
00012  */
00013 
00014 /* $Revision: 1.1 $ */
00015 
00016 #define NDIMS 2
00017 #define TOTAL_ELEMENTS 4
00018 
00019 /* the computational subroutine */
00020 void dbl_elem(unsigned short *x)
00021 {
00022   unsigned short scalar=2;
00023   int i,j;
00024 
00025   for(i=0;i<2;i++) {
00026     for(j=0;j<2;j++) {
00027       *(x+i*2+j) = scalar * *(x+i*2+j);
00028     }
00029   }
00030 }
00031 
00032 /* the gataway function */
00033 void mexFunction( int nlhs, mxArray *plhs[],
00034                   int nrhs, const mxArray *prhs[] )
00035 {
00036   const mwSize dims[]={2,2};
00037   unsigned char *start_of_pr;
00038   unsigned short data[]={1,2,3,4};
00039   size_t bytes_to_copy;
00040 
00041   (void) nlhs; (void) nrhs; (void) prhs;  /* unused parameters */
00042 
00043   /* call the computational subroutine */
00044   dbl_elem(data);
00045 
00046   /* create a 2-by-2 array of unsigned 16-bit integers */
00047   plhs[0] = mxCreateNumericArray(NDIMS,dims,mxUINT16_CLASS,mxREAL);
00048 
00049   /* populate the real part of the created array */
00050   start_of_pr = (unsigned char *)mxGetData(plhs[0]);
00051   bytes_to_copy = TOTAL_ELEMENTS * (size_t)mxGetElementSize(plhs[0]);
00052   memcpy(start_of_pr,data,bytes_to_copy);
00053 }

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