Finding the solution to something like this is tricky over the internet, but let's have a try. This post is comprised of questions rather than answers. However, I believe that if you answer them all you will find your bug without further help – or at least be 90% of the way there.
All these steps are sensible for debugging any type of MATLAB program.
Ensuring a clear workspace
Old versions of variables hanging around in the workspace can make it hard to debug. A mispelling in a variable name can cause an old version to be used by accident. Clear your workspace with clear
at the start of your debugging.
Running a simpler program
I have compiled libsvm, added the ovr_multiclass
addons, and I can succesfully run the following example script that I made up:
clear
% random train and test data
trainX = rand(10, 4);
trainY = randi(4, 10, 1);
testX = rand(10, 4);
testY = randi(4, 10, 1);
model=ovrtrain(trainY,trainX,'-c 8 -g 4');
[~,accuracy,~]=ovrpredict(testY,testX,model);
Can you run this, or do you get the same error as before? This sort of minimum working example is really useful for debugging. Using small, user-generated data ensures that the errors don't come from unexpected sources and helps to narrow down the cause.
Examining the models
cell array
You state that the error is occuring in this :
decv(:, i) = d * (2 * models{i}.Label(1) - 1); % ERROR IN THIS LINE
The critical part of the line is models{i}.Label(1)
. This is taking a cell array models
, and extracting the i
th item. This i
th item is expected to be a structure, with a field called Label
. Label
is expected to be a non-empty array, from which the first element can be extracted. The models
cell array is a field in the structure model
, which is passed in as a third argument to ovrpredict
.
After running my very simple test script above, I run the following diagnostics in the MATLAB command window:
>> models = model.models
models =
[1x1 struct]
[1x1 struct]
[1x1 struct]
[1x1 struct]
>> models{1}
ans =
Parameters: [5x1 double]
nr_class: 2
totalSV: 6
rho: -1.2122
Label: [2x1 double]
sv_indices: [6x1 double]
ProbA: []
ProbB: []
nSV: [2x1 double]
sv_coef: [6x1 double]
SVs: [6x4 double]
>> models{1}.Label
ans =
0
1
If you do the same, do you get the same result? If not, post your output to these commands in an edit to your question.
Debugging in the function
If the model looks OK but you are still getting the error, turn on the error debugger by typing this into the command window:
dbstop if error
When MATLAB encounters an error in a function it will now pause and allow you to inspect all the variables.
Run your program again (or mine, if you are getting the error with the minimum working example I posted). The program should pause when the error occurs. Your command prompt should change from >>
to K>>
.
Run through the steps above to example the model
cells array. Then try copying, pasting and running the line of code which gave the error in the command window. Try running a smaller portion of it, e.g. models{i}.Label(1)
then 2 * models{i}.Label(1) - 1
.
Type dbquit
to exit the debugger, and dbclear if error
to turn off the automatic debugging on error.
(See also my question below about the error message – make sure that the error is actually occuring where you think it is!)
Edit: Some extra questions
What version of MATLAB are you using? e.g. R2013a
If you type
which ovrpredict
into the MATLAB command line, do you see the path to the file that you expect? (i.e. the correct path to ovrpredict.m as you have it saved on your computer)Does your
ovrpredict.m
file (as pointed to bywhich ovrpredict
) contain exactly what you have pasted in your question? The error you are getting suggests their might be a tiny difference, e.g. an extra space.