7 #if MX_API_VER < 0x07030000 15 "Usage: libsvmwrite('filename', label_vector, instance_matrix);\n" 19 void libsvmwrite(
const char *filename,
const mxArray *label_vec,
const mxArray *instance_mat)
21 FILE *fp = fopen(filename,
"w");
22 int i, k, low, high, l;
24 int label_vector_row_num;
25 double *samples, *labels;
26 mxArray *instance_mat_col;
30 mexPrintf(
"can't open output file %s\n",filename);
36 mxArray *prhs[1], *plhs[1];
37 prhs[0] = mxDuplicateArray(instance_mat);
38 if(mexCallMATLAB(1, plhs, 1, prhs,
"transpose"))
40 mexPrintf(
"Error: cannot transpose instance matrix\n");
43 instance_mat_col = plhs[0];
44 mxDestroyArray(prhs[0]);
48 l = (int) mxGetN(instance_mat_col);
49 label_vector_row_num = (int) mxGetM(label_vec);
51 if(label_vector_row_num!=l)
53 mexPrintf(
"Length of label vector does not match # of instances.\n");
58 labels = mxGetPr(label_vec);
59 samples = mxGetPr(instance_mat_col);
60 ir = mxGetIr(instance_mat_col);
61 jc = mxGetJc(instance_mat_col);
65 fprintf(fp,
"%g", labels[i]);
67 low = (int) jc[i], high = (
int) jc[i+1];
69 fprintf(fp,
" %ld:%g", ir[k]+1, samples[k]);
79 int nrhs,
const mxArray *prhs[] )
85 if(!mxIsDouble(prhs[1]) || !mxIsDouble(prhs[2]))
87 mexPrintf(
"Error: label vector and instance matrix must be double\n");
91 mxGetString(prhs[0], filename, mxGetN(prhs[0])+1);
93 if(mxIsSparse(prhs[2]))
97 mexPrintf(
"Instance_matrix must be sparse\n");
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
void libsvmwrite(const char *filename, const mxArray *label_vec, const mxArray *instance_mat)