core/matlab/examples/refbook/sincall.c

00001 /* $Revision: 1.1 $ */
00002 /*=====================================================================
00003  * sincall.c
00004  *
00005  * example for illustrating how to use mexCallMATLAB
00006  * 
00007  * creates an mxArray and passes its associated  pointers (in this demo,
00008  * only pointer to its real part, pointer to number of rows, pointer to
00009  * number of columns) to subfunction fill() to get data filled up, then 
00010  * calls mexCallMATLAB to calculate sin function and plot the result.
00011  *
00012  * This is a MEX-file for MATLAB.
00013  * Copyright 1984-2006 The MathWorks, Inc.
00014  *===================================================================*/
00015 #include "mex.h"
00016 #define MAX 1000
00017 
00018 /* subroutine for filling up data */
00019 void fill( double *pr, mwSize *pm, mwSize *pn, mwSize max )
00020 {
00021     mwSize i;  
00022     /* you can fill up to max elements, so (*pr)<=max */
00023     *pm = max/2;
00024     *pn = 1;
00025     for (i=0; i < (*pm); i++) 
00026       pr[i]=i*(4*3.14159/max);
00027 }
00028 
00029 /* gateway function */
00030 void mexFunction( int nlhs, mxArray *plhs[],
00031                   int nrhs, const mxArray *prhs[] )
00032 {
00033     mwSize m, n, max=MAX;
00034     mxArray *rhs[1], *lhs[1];
00035 
00036     (void) nlhs; (void) plhs;    /* unused parameters */
00037     (void) nrhs; (void) prhs;
00038 
00039     rhs[0] = mxCreateDoubleMatrix(max, 1, mxREAL);
00040 
00041     /* pass the pointers and let fill() fill up data */
00042     fill(mxGetPr(rhs[0]), &m, &n, MAX);
00043     mxSetM(rhs[0], m);
00044     mxSetN(rhs[0], n);
00045     
00046 
00047     /* get the sin wave and plot it */
00048     mexCallMATLAB(1, lhs, 1, rhs, "sin");
00049     mexCallMATLAB(0, NULL, 1, lhs, "plot");
00050 
00051     /* cleanup allocated memory */
00052     mxDestroyArray(rhs[0]);
00053     mxDestroyArray(lhs[0]);
00054      
00055     return;
00056 }

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