9

How to get started with libsvm under MATLAB?

I've downloaded the library, and extracted it in C:\Program Files\MATLAB\R2012a\toolbox\, but then I don't know how to use it in MATLAB.

Robert Pollak
  • 3,751
  • 4
  • 30
  • 54
user2157806
  • 319
  • 2
  • 8
  • 17
  • read the `README` file located in the extracted archive under `matlab` subfolder. You will need to compile the MEX-files, then add the folder to the MATLAB search path – Amro Mar 21 '13 at 21:22
  • Have you read the README file that is located inside matlab folder? There are some instructions about how to use it in matlab. You'd need to run a make command inside matlab among other things. – Pedrom Mar 21 '13 at 21:24
  • 1
    but how to compile The MEX-files? I tried this [link](http://stackoverflow.com/questions/13954007/how-to-set-up-libsvm-matlab-interface?rq=1) but it's error in matlab. and also I tried mex -setup but there's error "Error using mex (line 206) Unable to complete successfully." – user2157806 Mar 21 '13 at 21:36
  • @user2157806: You need to have a [supported](http://www.mathworks.com/support/compilers/current_release/) C++ compiler. What Windows are you on? Run `mex.getCompilerConfigurations('Any','Installed')` to get a list of installed compilers that are recognized by MATLAB – Amro Mar 21 '13 at 21:44
  • Are you using a 32- or 64-bit version of Matlab? – erikced Mar 21 '13 at 21:45
  • Iam using 64 bit version of matlab and I run mex.getCompilerConfigurations('Any','Installed')and the output ans = 1x0 empty mex.CompilerConfiguration Package: mex Properties: Name Manufacturer Language Version Location Details Methods – user2157806 Mar 21 '13 at 21:51
  • @user2157806: For 64-bit Windows/MATLAB, you need either Visual Studio Professional edition (with the "X64 compiler and tools" component installed), or use the freely available VS2010 Express edition along with the latest Windows SDK. This is explained in the page I linked to in my comment above – Amro Mar 21 '13 at 21:56
  • On the other hand, I think libsvm already provides pre-compiled 64-bit Windows MEX-files in the package you downloaded (look inside the `windows` sub-folder of the extracted archive, the files are named: `*.mexw64`) – Amro Mar 21 '13 at 21:59
  • iam beginner in matlab and I still dont understand what do u mean libsvm already provide pre-compiled 64-bit MEX-files in the package ? I hve installed Microsoft Windows SDK 7.1 and Microsoft Visual C++ 2010 Professional. what should i do now? – user2157806 Mar 21 '13 at 22:06
  • 1
    First try the easier method. Lets say you extracted libsvm in `C:\libsvm`. Start by telling MATLAB where to find the library by running: `addpath('C:\libsvm\windows')` to have the compiled MEX-files available in the search path. Now you can test the functions with something like: `svmtrain(double(rand(10,1)>0.5),rand(10,5),'-c 1 -g 0.1')` – Amro Mar 21 '13 at 22:12
  • yap. now i can run it .. but why when i run mex -setup , it's still error Error using mex (line 206) Unable to complete successfully. it will be the problem when i using libsvm? – user2157806 Mar 21 '13 at 22:21
  • @user2157806: I cant say whats wrong with your setup... Anyway, since those pre-compiled MEX-files are working for you, you don't need to do the compilation yourself, libsvm should be working fine now :) – Amro Mar 21 '13 at 22:28
  • @user2157806: I combined the comments into an answer – Amro Mar 21 '13 at 22:38

2 Answers2

30

Download and extract libsvm in a directory of your choosing, say C:\libsvm

As described in the C:\libsvm\matlab\README file, first you have to make sure a supported C/C++ compiler is installed. Note that on 64-bit systems, you need the correct 64-bit version of the compiler (e.g. Windows SDK is needed for Visual Studio Express edition)

>> mex -setup

Once you have selected a compiler, you need to compile the MEX-files:

>> cd('C:\libsvm\matlab')
>> make

Finally add the folder with the generated binaries to the MATLAB search path:

>> addpath('C:\libsvm\matlab')

Test the library with a simple example (fake data):

>> labels = double(rand(10,1)>0.5);
>> data = rand(10,5);
>> model = svmtrain(labels, data, '-s 0 -t 2 -c 1 -g 0.1')

Note that the current version of libsvm includes pre-compiled 64-bit MEX-files for Windows. The binaries are located in C:\libsvm\windows\*.mexw64 (copy those to the matlab subfolder from above)

Amro
  • 123,847
  • 25
  • 243
  • 454
  • 1
    Thanks alot, it worked for me for Windows OS. Can you please help me to run the same thing for Mac OS? – Khalid Usman Feb 28 '14 at 06:04
  • @Amro Add some more details about testing, coz I'm facing problems with option `-b` i.e. probability. The result is showing same prob distribution among 5 classes for all tests. – kAmol Mar 30 '14 at 03:22
2

For me I didn't need to recompile the libsvm files(it did cause some problems with the .net framwork and windows SDK) I only used the already compiled files and added them to a new folder by following the steps mentioned here minus the make step.

So to summarize:

1- I think you need to create "libsvm" folder under "C:\Program Files\MATLAB\R2014b\toolbox\".

2- Then copy the *.mexw64 files from the "libsvm-3.21\windows" folder to the new folder.

3- finally add the libsvm folder you just created to matlab bath by clicking the set path button in home and adding the new folder with the path "C:\Program Files\MATLAB\R2014b\toolbox\libsvm"

Community
  • 1
  • 1
Sa8923
  • 21
  • 1