libsvmwrite.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include "mex.h"
00005 
00006 #ifdef MX_API_VER
00007 #if MX_API_VER < 0x07030000
00008 typedef int mwIndex;
00009 #endif
00010 #endif
00011 
00012 void exit_with_help()
00013 {
00014         mexPrintf(
00015         "Usage: libsvmwrite('filename', label_vector, instance_matrix);\n"
00016         );
00017 }
00018 
00019 void libsvmwrite(const char *filename, const mxArray *label_vec, const mxArray *instance_mat)
00020 {
00021         FILE *fp = fopen(filename,"w");
00022         int i, k, low, high, l;
00023         mwIndex *ir, *jc;
00024         int label_vector_row_num;
00025         double *samples, *labels;
00026         mxArray *instance_mat_col; // instance sparse matrix in column format
00027 
00028         if(fp ==NULL)
00029         {
00030                 mexPrintf("can't open output file %s\n",filename);                      
00031                 return;
00032         }
00033 
00034         // transpose instance matrix
00035         {
00036                 mxArray *prhs[1], *plhs[1];
00037                 prhs[0] = mxDuplicateArray(instance_mat);
00038                 if(mexCallMATLAB(1, plhs, 1, prhs, "transpose"))
00039                 {
00040                         mexPrintf("Error: cannot transpose instance matrix\n");
00041                         return;
00042                 }
00043                 instance_mat_col = plhs[0];
00044                 mxDestroyArray(prhs[0]);
00045         }
00046 
00047         // the number of instance
00048         l = (int) mxGetN(instance_mat_col);
00049         label_vector_row_num = (int) mxGetM(label_vec);
00050 
00051         if(label_vector_row_num!=l)
00052         {
00053                 mexPrintf("Length of label vector does not match # of instances.\n");
00054                 return;
00055         }
00056 
00057         // each column is one instance
00058         labels = mxGetPr(label_vec);
00059         samples = mxGetPr(instance_mat_col);
00060         ir = mxGetIr(instance_mat_col);
00061         jc = mxGetJc(instance_mat_col);
00062 
00063         for(i=0;i<l;i++)
00064         {
00065                 fprintf(fp,"%g", labels[i]);
00066 
00067                 low = (int) jc[i], high = (int) jc[i+1];
00068                 for(k=low;k<high;k++)
00069                         fprintf(fp," %ld:%g", ir[k]+1, samples[k]);             
00070 
00071                 fprintf(fp,"\n");
00072         }
00073 
00074         fclose(fp);
00075         return;
00076 }
00077 
00078 void mexFunction( int nlhs, mxArray *plhs[],
00079                 int nrhs, const mxArray *prhs[] )
00080 {       
00081         // Transform the input Matrix to libsvm format
00082         if(nrhs == 3)
00083         {
00084                 char filename[256];
00085                 if(!mxIsDouble(prhs[1]) || !mxIsDouble(prhs[2]))
00086                 {
00087                         mexPrintf("Error: label vector and instance matrix must be double\n");                  
00088                         return;
00089                 }
00090                 
00091                 mxGetString(prhs[0], filename, mxGetN(prhs[0])+1);              
00092 
00093                 if(mxIsSparse(prhs[2]))
00094                         libsvmwrite(filename, prhs[1], prhs[2]);
00095                 else
00096                 {
00097                         mexPrintf("Instance_matrix must be sparse\n");                  
00098                         return;
00099                 }
00100         }
00101         else
00102         {
00103                 exit_with_help();               
00104                 return;
00105         }
00106 }


haf_grasping
Author(s): David Fischinger
autogenerated on Thu Jun 6 2019 18:35:09