core/matlab/examples/mx/mxcalcsinglesubscript.c

00001 /*=================================================================
00002  * mxcalcsinglesubscript.c 
00003  *
00004  * This is an example of how to use mxCalcSingleSubscript.  To run
00005  * this example, you pass in a N-dimensional array of doubles and
00006  * MATLAB indices to the element you would like returned. It will
00007  * return the element you selected. The number of indices you pass in
00008  * must match the number of dimensions in your array. The array must
00009  * consist of real values.  This example demonstrates that MATLAB uses
00010  * 1 based indexing and C uses 0 based indexing.  It details how to
00011  * convert from MATLAB to vector *indexing.
00012  *
00013  * This is a MEX-file for MATLAB.  
00014  * Copyright 1984-2006 The MathWorks, Inc.
00015  * All rights reserved.
00016  *=================================================================*/
00017  /* $Revision: 1.1 $ */
00018 
00019 #include "mex.h"
00020 
00021 void
00022 mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
00023 {
00024     mwSize  nsubs, x; 
00025     mwIndex index;
00026     double  *temp;
00027     mwIndex  *subs;
00028       
00029     /* Check for proper number of input and output arguments */    
00030     if (nrhs != 2) {
00031     mexErrMsgTxt("Two input arguments required.");
00032     } 
00033     if (nlhs > 1) {
00034     mexErrMsgTxt("Too many output arguments.");
00035     }
00036         
00037     /* Check data type of first input argument */
00038     if (!mxIsDouble(prhs[0])) {
00039     mexErrMsgTxt("First input argument must be a double.");
00040     }
00041     /* Check data type of second argument */
00042     if (!mxIsDouble(prhs[1]) || mxIsComplex(prhs[1])) {
00043     mexErrMsgTxt("Second input argument must be a real double.");
00044     }
00045     /* Get the number of dimensions in array */
00046     nsubs=mxGetNumberOfDimensions(prhs[0]);
00047       
00048     /* Check for the correct number of indices  */
00049     if (mxGetNumberOfElements(prhs[1]) != nsubs){
00050     mexErrMsgTxt("You must specify an index for each dimension.");
00051     }
00052 
00053     /* Allocate memory for the subs array on the fly */
00054     subs=mxCalloc(nsubs,sizeof(mwIndex));
00055       
00056     /* Get the indices and account for the fact that MATLAB is 1
00057        based and C is zero based.  While doing this, check to make
00058        sure that an index was not specified that is larger than size
00059        of input array */
00060 
00061     temp=mxGetPr(prhs[1]);
00062        
00063     for (x=0;x<nsubs;x++){
00064     subs[x]=(mwIndex)temp[x]-1;
00065     if (temp[x]> ((mxGetDimensions(prhs[0]))[x]) ){
00066         mxFree(subs);
00067         mexErrMsgTxt("You indexed above the size of the array.");
00068     }
00069     }
00070 
00071     /* Find the index of location selected.  Note, for example, that
00072        (3,4) in MATLAB corresponds to (2,3) in C. */
00073     index = mxCalcSingleSubscript(prhs[0], nsubs, subs);
00074       
00075     /* Create the output array */
00076     plhs[0] = mxCreateDoubleMatrix(1, 1, mxIsComplex(prhs[0]) ? mxCOMPLEX : mxREAL);
00077       
00078     /* Free allocated memory*/
00079     mxFree(subs);
00080       
00081     /* Assign value in C based array to plhs. */
00082     mxGetPr(plhs[0])[0]= mxGetPr(prhs[0])[index];
00083     if (mxIsComplex(prhs[0])) {
00084     mxGetPi(plhs[0])[0]= mxGetPi(prhs[0])[index];
00085     }
00086 }

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