/* * main.cpp * * Matlab interface for writing SVMWeights to an XML file * Input: array (9216x8), ni, mu * Output: nothing to Matlab, creates/overwrites an XML file: SVMWeights_ni_mu.xml * * Created by Andrew Salamon on Tue May 9 2006. * * Copyright (c) 2006 Machine Perception Laboratory * University of California San Diego. * Please read the license information at the end of this file. */ #include #include #include #include #include //#include //#include //#include #include #include using namespace std; void writeOneArray( const mxArray *array, string &prefix, int ni, int mu, int prec, string &filename, string &datestamp, string & certVersion ); void writeOneArrayBinary( const mxArray *array, string &prefix, int ni, int mu, int prec ); /** A mex file for creating SVM weights files for use by CERT. * This function requires six arguments and can take two optional ones: * -# mxArray: A Matlab array (9216x8). One slice of the full SVMWeights data * -# int: ni * -# int: mu * -# string: Source file name * -# string: A datestamp * -# string: The CERT version * -# int: The precision to be used for output * -# string: Prefix to be used for the xml file names */ void mexFunction(int nlhs,mxArray*plhs[],int nrhs, const mxArray*prhs[]) { static string prefix( "SVMWeights" ); // check number of input/output variables if (nlhs != 0 ) mexErrMsgTxt("SVMExport has no outputs"); if (nrhs < 3) mexErrMsgTxt("SVMExport: requires six inputs: 9216x8 array, ni, mu, source filename, datestamp (as a string), CERT version (as a string). Two extra arguments are allowed an optional precision, and an optional filename prefix string."); int ni = static_cast(mxGetScalar(prhs[1])); int mu = static_cast(mxGetScalar(prhs[2])); string filename( mxArrayToString( prhs[3] ) ); string datestamp( mxArrayToString( prhs[4] ) ); string certVersion( mxArrayToString( prhs[5] ) ); int prec = 6; int index = 5; if( nrhs > index ) { #ifdef DEBUG mexWarnMsgTxt("More than six args."); #endif if( mxCHAR_CLASS == mxGetClassID( prhs[index] ) ) { char *str = mxArrayToString( prhs[index] ); #ifdef DEBUG mexWarnMsgTxt("Arg six is a string."); mexWarnMsgTxt(str); #endif if( strlen( str ) > 0 ) prefix = str; } else { prec = static_cast(mxGetScalar(prhs[index])); } } ++index; if( nrhs > index ) { #ifdef DEBUG mexWarnMsgTxt("More than seven args."); #endif if( mxCHAR_CLASS == mxGetClassID( prhs[index] ) ) { char *str = mxArrayToString( prhs[index] ); #ifdef DEBUG mexWarnMsgTxt("Arg seven is a string."); mexWarnMsgTxt(str); #endif if( strlen( str ) > 0 ) prefix = str; } else { prec = static_cast(mxGetScalar(prhs[index])); } } writeOneArray( prhs[0], prefix, ni, mu, prec, filename, datestamp, certVersion ); // writeOneArrayBinary( prhs[0], prefix, ni, mu, prec ); } void writeOneArray( const mxArray *array, string &prefix, int ni, int mu, int prec, string &filename, string &datestamp, string & certVersion ) { double *realArray = mxGetPr(array); int rowmax = static_cast(mxGetM(array)); int colmax = static_cast(mxGetN(array)); std::ostringstream fNameStream; fNameStream << prefix << "_" << ni << "_" << mu << ".xml"; string fName( fNameStream.str() ); ofstream file( fName.c_str(), ios::out); file << "" << endl; // file << "" << endl; file << setprecision(prec); for( int row=0; row"; for( int col=0; col" << endl; } file << "" << endl; } void writeOneArrayBinary( const mxArray *array, string &prefix, int ni, int mu, int prec ) { double *realArray = mxGetPr(array); int rowmax = static_cast(mxGetM(array)); int colmax = static_cast(mxGetN(array)); std::ostringstream fNameStream; fNameStream << prefix << "_" << ni << "_" << mu << ".xml"; string fName( fNameStream.str() ); ofstream file( fName.c_str(), ios::out | ios::binary | ios::trunc ); file << "SVMWeights category=" << prefix << " ni=" << ni << " mu=" << mu << " colcount=" << colmax << " rowcount=" << rowmax << endl; for( int row=0; row