core/matlab/examples/mex/mexget.c

00001 /*=================================================================
00002  * mexget.c 
00003  *
00004  * This example demonstrates how to use mexGet and mexSet.  The input
00005  * to this function is a handle graphics handle.  mexget.c gets the
00006  * Color property of the handle that was passed into the function. It
00007  * then changes the colors, and uses mexSet to set the Color property
00008  * of the handle to the new color values.
00009  *
00010  *
00011  * This is a MEX-file for MATLAB.  
00012  * Copyright 1984-2006 The MathWorks, Inc.  
00013  * All rights reserved.
00014  *=================================================================*/
00015 
00016 /* $Revision: 1.1 $ */
00017 #include "mex.h"
00018 
00019 #define RED   0
00020 #define GREEN 1
00021 #define BLUE  2
00022 
00023 void mexFunction(int nlhs, mxArray *plhs[], int nrhs,
00024          const mxArray *prhs[])
00025 {
00026     double         handle; 
00027     const mxArray *color_array_ptr;
00028     mxArray       *value;
00029     double        *color; 
00030 
00031     (void) plhs;      /* unused parameters */
00032 
00033     /* Assume that the first input argument is a graphics
00034        handle. Check to make sure the input is a double and that only
00035        one input is specified.*/
00036     if(nrhs != 1 || !mxIsDouble(prhs[0])){
00037     mexErrMsgTxt("Must be called with a valid handle");
00038     }    
00039     /* Check for the correct number of ouputs */
00040     if(nlhs > 1){
00041     mexErrMsgTxt("Too many output arguments.");
00042     }
00043     /* Check to make sure input argument is a scalar */
00044 
00045     if (mxGetN(prhs[0]) != 1 || mxGetM(prhs[0]) !=1){
00046       mexErrMsgTxt("Input must be a scalar handle value.\n");
00047     }
00048     /* Get the handle */
00049     handle = mxGetScalar(prhs[0]);
00050     
00051     /* Get the "Color" property associated with this handle. */
00052     color_array_ptr = mexGet(handle, "Color");
00053     if (color_array_ptr == NULL)
00054       mexErrMsgTxt("Could not get this handle property");
00055     
00056    /* Make copy of "Color" propery */
00057     value = mxDuplicateArray(color_array_ptr);
00058     
00059     /* The returned "Color" property is a 1-by-3 matrix of 
00060        primary colors. */ 
00061     color = mxGetPr(value);
00062     
00063     /* Change the color values */
00064     color[RED] = (1 + color[RED]) /2;
00065     color[GREEN] = color[GREEN]/2;
00066     color[BLUE] = color[BLUE]/2;
00067     
00068     /* Reset the "Color" property to use the new color. */
00069     if(mexSet(handle, "Color", value))
00070       mexErrMsgTxt("Could not set a new 'Color' property.");
00071 }
00072 
00073     
00074 

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