10 #if MX_API_VER < 0x07030000 15 #define max(x,y) (((x)>(y))?(x):(y)) 18 #define min(x,y) (((x)<(y))?(x):(y)) 24 "Usage: [label_vector, instance_matrix] = libsvmread('filename');\n" 30 plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL);
31 plhs[1] = mxCreateDoubleMatrix(0, 0, mxREAL);
44 while(strrchr(
line,
'\n') == NULL)
48 len = (int) strlen(
line);
58 int max_index, min_index, inst_max_index, i;
60 FILE *fp = fopen(filename,
"r");
64 double *labels, *samples;
68 mexPrintf(
"can't open input file %s\n",filename);
89 idx = strtok(NULL,
":");
90 val = strtok(NULL,
" \t");
95 index = (int) strtol(idx,&endptr,10);
96 if(endptr == idx || errno != 0 || *endptr !=
'\0' || index <= inst_max_index)
98 mexPrintf(
"Wrong input format at line %d\n",l+1);
103 inst_max_index =
index;
105 min_index =
min(min_index, index);
108 max_index =
max(max_index, inst_max_index);
114 plhs[0] = mxCreateDoubleMatrix(l, 1, mxREAL);
117 plhs[1] = mxCreateSparse(max_index-min_index+1, l, elements, mxREAL);
119 plhs[1] = mxCreateSparse(max_index, l, elements, mxREAL);
121 labels = mxGetPr(plhs[0]);
122 samples = mxGetPr(plhs[1]);
123 ir = mxGetIr(plhs[1]);
124 jc = mxGetJc(plhs[1]);
129 char *idx, *val, *
label;
134 label = strtok(
line,
" \t\n");
137 mexPrintf(
"Empty line at line %d\n",i+1);
141 labels[i] = strtod(label,&endptr);
142 if(endptr == label || *endptr !=
'\0')
144 mexPrintf(
"Wrong input format at line %d\n",i+1);
152 idx = strtok(NULL,
":");
153 val = strtok(NULL,
" \t");
157 ir[k] = (mwIndex) (strtol(idx,&endptr,10) - min_index);
160 samples[k] = strtod(val,&endptr);
161 if (endptr == val || errno != 0 || (*endptr !=
'\0' && !isspace(*endptr)))
163 mexPrintf(
"Wrong input format at line %d\n",i+1);
176 mxArray *rhs[1], *lhs[1];
178 if(mexCallMATLAB(1, lhs, 1, rhs,
"transpose"))
180 mexPrintf(
"Error: cannot transpose problem\n");
189 int nrhs,
const mxArray *prhs[] )
195 mxGetString(prhs[0], filename, mxGetN(prhs[0]) + 1);
199 mexPrintf(
"Error: filename is NULL\n");
void read_problem(const char *filename, mxArray *plhs[])
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
static char * readline(FILE *input)
static void fake_answer(mxArray *plhs[])