• API Main Page
  • Documentation
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

matlab/mex/mex_utils.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2010 by Matthias Ihrke   *
00003  *   ihrke@nld.ds.mpg.de
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #include <string.h>
00022 
00023 #include "mex_utils.h"
00024 #include "linalg.h"
00025 
00026 
00027 
00028 mxArray *create_mex_double( double val )
00029 {
00030   mxArray *array = mxCreateNumericMatrix( 1, 1, mxDOUBLE_CLASS, mxREAL );
00031   double *df_ptr = (double*)mxGetData( array );
00032   *df_ptr = val;
00033   return array;
00034 }
00035 
00036 mxArray *create_mex_string( const char *str )
00037 {
00038     char *buf = (char*)mxCalloc( strlen( str )+1, sizeof(char) );
00039     strcpy( buf, str );
00040     return mxCreateString( buf );
00041 }
00042 
00043 char *get_mex_string( const mxArray *arg )
00044 { 
00045   /* Input must be a string. */
00046   if (mxIsChar(arg) != 1)
00047     mexErrMsgTxt("Input must be a string.");
00048 
00049   /* Input must be a row vector. */
00050   if (mxGetM(arg) != 1)
00051     mexErrMsgTxt("Input must be a row vector.");
00052     
00053   /* Get the length of the input string. */
00054   int buflen = (mxGetM(arg) * mxGetN(arg)) + 1;
00055 
00056   /* Allocate memory for input and output strings. */
00057   char *str = (char*)mxCalloc(buflen, sizeof(char));
00058   
00059   /* Copy the string data from prhs[0] into a C string 
00060    * input_buf. */
00061   int status = mxGetString(arg, str, buflen);
00062 
00063   if (status != 0) 
00064     mexWarnMsgTxt("Not enough space. String is truncated.");
00065  
00066   return str;
00067 }
00068 
00069 double get_mex_double( const mxArray *arg )
00070 {
00071     double *data = mxGetPr( arg );   
00072     return data[0];
00073 }
00074 
00075 float get_mex_float( const mxArray *arg )
00076 {
00077     float* data = (float*)mxGetPr( arg );   
00078     return data[0];
00079 }
00080 
00081 
00082 int set_mex_field( mxArray *arg, const char *field_name, mxArray *value )
00083 { 
00084     int fnum = mxGetFieldNumber( arg, field_name );
00085     if( fnum == -1 ) {
00086         fnum = mxAddField( arg, field_name );
00087     }
00088     mxSetFieldByNumber( arg, 0, fnum, value );
00089 
00090     return fnum;
00091 }
00092 
00093 bool is_mex_scalar( const mxArray *arg )
00094 {
00095   if( arg == NULL ) return false;
00096   return mxIsNumeric( arg ) && mxGetN( arg ) == 1 && mxGetM( arg ) == 1;
00097 }
00098 
00099 bool is_mex_fid( const mxArray *arg )
00100 {
00101   if( arg == NULL ) return false;
00102   return mxIsNumeric( arg ) && (mxGetN( arg ) == 1 || mxGetN( arg ) == 2) && mxGetM( arg ) == 1;
00103 }
00104 
00105 bool is_mex_matrix( mxArray *arg ){
00106   if( arg == NULL ) return false;
00107   return mxIsDouble( arg ) && mxGetNumberOfDimensions(arg)==2;
00108 }
00109 
00114 Array* mex_mxarray_to_array( mxArray * mxa ){
00115   Array *out;
00116   int ndim=mxGetNumberOfDimensions(mxa);
00117   uint *dims=mxGetDimensions(mxa);
00118   double *ptr=(double*)mxGetPr(mxa);
00119   out = array_new( DOUBLE, ndim, dims );
00120 
00121   ulong i;
00122   uint *idx=(uint*)malloc(ndim*sizeof(idx));;
00123   uint cidx;
00124 
00125   /* col major to row major */
00126   for( i=0; i<array_NUMEL( out ); i++ ){
00127      array_calc_rowindex( i, out->size, out->ndim, idx );
00128      cidx=mxCalcSingleSubscript( mxa, ndim, idx);
00129      array_INDEX1( out, double, i) = ptr[cidx];
00130   }
00131   free( idx );
00132 
00133   array_dimred( out );
00134 
00135   return out;
00136 }
00137 
00142 mxArray* mex_array_to_mxarray( Array * a ){
00143   mxArray *mxa =mxCreateNumericArray(a->ndim, a->size,
00144                                                  mxDOUBLE_CLASS, mxREAL );
00145   
00146   Array *tmp = array_convert_rowcolmajor( a, TRUE );
00147   memcpy( mxGetPr(mxa), tmp->data, tmp->nbytes );
00148   array_free( tmp );
00149   
00150   return mxa;
00151 }
00152 
00157 mxArray* mex_int_array_to_mxarray( Array * a ){
00158   mxClassID intclass;
00159   switch( sizeof(int) ){
00160   case 1:
00161      intclass=mxINT8_CLASS; break;
00162   case 2:
00163      intclass=mxINT16_CLASS; break;
00164   case 4:
00165      intclass=mxINT32_CLASS; break;
00166   case 8:
00167      intclass=mxINT64_CLASS; break;
00168   default:
00169      errprintf("what's that?\n");
00170      intclass=mxINT64_CLASS; break;
00171   }
00172   mxArray *mxa =mxCreateNumericArray(a->ndim, a->size,
00173                                                  intclass, mxREAL );
00174   
00175   Array *tmp = array_convert_rowcolmajor( a, TRUE );
00176   memcpy( mxGetPr(mxa), tmp->data, tmp->nbytes );
00177   array_free( tmp );
00178   
00179   return mxa;
00180 }

Generated on Fri Jun 25 2010 14:10:16 for libeegtools by  doxygen 1.7.0