core/matlab/examples/mx/mxmalloc.c

00001 /*=================================================================
00002  * mxmalloc.c
00003  * 
00004  * This function takes a MATLAB string as an argument and copies it in 
00005  * NULL terminated ANSI C string.
00006  *
00007  * This is a MEX-file for MATLAB.  
00008  * Copyright 1984-2006 The MathWorks, Inc.
00009  * All rights reserved.
00010  *=================================================================*/
00011 
00012 /* $Revision: 1.1 $ */
00013 #include "mex.h"
00014    
00015 void
00016 mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
00017 {
00018     char *buf;
00019     mwSize buflen;
00020     int status;
00021 
00022     (void) plhs;    /* unused parameters */
00023     
00024     /* Check for proper number of input and output arguments */
00025     if (nrhs != 1) { 
00026     mexErrMsgTxt("One input argument required.");
00027     } 
00028     if (nlhs > 1) {
00029     mexErrMsgTxt("Too many output arguments.");
00030     }
00031     
00032     /* Check for proper input type */
00033     if (!mxIsChar(prhs[0]) || (mxGetM(prhs[0]) != 1 ) )  {
00034     mexErrMsgTxt("Input argument must be a string.");
00035     }
00036     
00037     /* Find out how long the input string is.  Allocate enough memory
00038        to hold the converted string.  NOTE: MATLAB stores characters
00039        as 2 byte unicode ( 16 bit ASCII) on machines with multi-byte
00040        character sets.  You should use mxChar to ensure enough space
00041        is allocated to hold the string */
00042     
00043     buflen = mxGetN(prhs[0])*sizeof(mxChar)+1;
00044     buf = mxMalloc(buflen);
00045     
00046     /* Copy the string data into buf. */ 
00047     status = mxGetString(prhs[0], buf, buflen);   
00048     mexPrintf("The input string is:  %s\n", buf);
00049     /* NOTE: You could add your own code here to manipulate 
00050        the string */
00051     
00052     /* When finished using the string, deallocate it. */
00053     mxFree(buf);
00054     
00055 }
00056 
00057 
00058 
00059 
00060 
00061 
00062 

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