testHelperDataConversionMex.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * @file GPHIKClassifierMex.cpp
  3. * @author Alexander Freytag
  4. * @date 07-01-2014 (dd-mm-yyyy)
  5. * @brief Matlab-Interface of our GPHIKClassifier, allowing for training, classification, optimization, variance prediction, incremental learning, and storing/re-storing.
  6. */
  7. // STL includes
  8. #include <math.h>
  9. #include <matrix.h>
  10. #include <mex.h>
  11. // NICE-core includes
  12. #include <core/vector/MatrixT.h>
  13. #include <core/vector/VectorT.h>
  14. // Interface for conversion between Matlab and C objects
  15. #include "gp-hik-core/matlab/classHandleMtoC.h"
  16. #include "gp-hik-core/matlab/ConverterMatlabToNICE.h"
  17. #include "gp-hik-core/matlab/ConverterNICEToMatlab.h"
  18. #include "HelperDataConversionMex.h"
  19. using namespace std; //C basics
  20. using namespace NICE; // nice-core
  21. // MAIN MATLAB FUNCTION
  22. void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  23. {
  24. // get the command string specifying what to do
  25. if (nrhs < 1)
  26. mexErrMsgTxt("No commands and options passed... Aborting!");
  27. if( !mxIsChar( prhs[0] ) )
  28. mexErrMsgTxt("First argument needs to be the command, ie.e, the unit test method to call... Aborting!");
  29. std::string cmd = MatlabConversion::convertMatlabToString( prhs[0] );
  30. // in all other cases, there should be a second input,
  31. // which the be the class instance handle
  32. if (nrhs < 2)
  33. {
  34. mexErrMsgTxt("Second input should be some kind of matrix variable");
  35. return;
  36. }
  37. if (nlhs < 1)
  38. {
  39. mexErrMsgTxt("No return value defined, possible loss of data... Aborting!");
  40. }
  41. ////////////////////////////////////////
  42. // Check which method to call //
  43. ////////////////////////////////////////
  44. if ( !strcmp("convertDoubleMatrixToExamples", cmd.c_str() ) )
  45. {
  46. if (nrhs != 3)
  47. {
  48. mexErrMsgTxt("needs 2 matrix inputs, first the training features, second the sample labels");
  49. return;
  50. }
  51. const mxArray *t_pArrTrainData = prhs[1];
  52. const mxArray *t_pArrTrainLabels = prhs[2];
  53. OBJREC::Examples t_ExamplesTrain;
  54. bool bConversionSuccess = MatlabConversion::convertDoubleRawPointersToExamples( t_pArrTrainData, t_pArrTrainLabels, t_ExamplesTrain );
  55. std::cerr << "Examples size: " << t_ExamplesTrain.size() << std::endl;
  56. for(int i=0; i< t_ExamplesTrain.size(); i++)
  57. {
  58. int iClass = t_ExamplesTrain[i].first;
  59. OBJREC::Example &t_Example = t_ExamplesTrain[i].second ;
  60. std::cerr << "Example["<<i<<"]" << "L:" << iClass << " data: "<< *t_Example.vec << std::endl;
  61. }
  62. // clean up
  63. for(int i=0; i< t_ExamplesTrain.size(); i++)
  64. {
  65. OBJREC::Example &t_Example = t_ExamplesTrain[i].second;
  66. if (t_Example.vec != NULL )
  67. {
  68. delete t_Example.vec;
  69. t_Example.vec = NULL;
  70. }
  71. }
  72. // output
  73. plhs[0] = mxCreateLogicalScalar( bConversionSuccess );
  74. return;
  75. }
  76. // Got here, so command not recognized
  77. std::string errorMsg (cmd.c_str() );
  78. errorMsg += " -- command not recognized.";
  79. mexErrMsgTxt( errorMsg.c_str() );
  80. }