core/matlab/examples/mx/mxgetnzmax.c

00001 /*=================================================================
00002  * mxgetnzmax.c
00003  *
00004  * This example illustrates how to use mxGetNzMax.  It takes a sparse
00005  * matrix as an input argument and it displays the number of nonzero
00006  * elements in the input argument and the maximum number of nonzero
00007  * elements that can be stored.  The maximum number of elements that
00008  * can be stored is the value of nzmax.
00009  *
00010  * This is a MEX-file for MATLAB.  
00011  * Copyright 1984-2006 The MathWorks, Inc.  
00012  * All rights reserved.
00013  *=================================================================*/
00014 /* $Revision: 1.1 $ */
00015 #include "mex.h"
00016 
00017 void
00018 mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
00019 {
00020     mwSize nzmax; 
00021     mwSize columns;
00022     mwIndex nnz;
00023     
00024     (void) plhs;    /* unused parameter */
00025     
00026     /* Check for proper number of input and output arguments */    
00027     if (nrhs != 1) {
00028     mexErrMsgTxt("One input argument required.");
00029     } 
00030     if(nlhs > 1){
00031     mexErrMsgTxt("Too many output arguments.");
00032     }
00033     
00034     if (!mxIsSparse(prhs[0]))  {
00035     mexErrMsgTxt("Input argument must be a sparse array.");
00036     } 
00037     nzmax = mxGetNzmax(prhs[0]);
00038     columns = mxGetN(prhs[0]);
00039     
00040     /* NOTE: nnz is the actual number of nonzeros and is stored as the
00041        last element of the jc array where the size of the jc array is the
00042        number of columns + 1 */
00043     nnz = *(mxGetJc(prhs[0]) + columns);
00044     
00045     mexPrintf("Contains %d nonzero elements.\n", nnz);
00046     mexPrintf("Can store up to %d nonzero elements.\n", nzmax);
00047 }
00048 

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