How to create mex files for libsvm C files - matlab

I have dowloaded libsvm from http://www.csie.ntu.edu.tw/~cjlin/libsvm/
I unziped and copied the libsvm folder to c:..../documents/MATLAB folder. includedmex.h to all C files. Inside libsvm-> I have different folders. also in that Matlab folder I have files like libsvmread.c , libsvmwrite.c , makefile, make.m, svmtrain ,svmpredict etc...
how I can use these files in matlab? Can any one help me to create .mex files for these files.
I read and tried many times ..it is not working correctly for me. I need to do svmensemble creation in matlab.can anyone help me ASAP.

Have a look at the README file:
Installation and Data Format
On Unix systems, type make' to build thesvm-train' and
`svm-predict' programs. Run them without arguments to show the usages
of them.
On other systems, consult Makefile' to build them (e.g., see
'Building Windows binaries' in this file) or use the pre-built
binaries (Windows binaries are in the directorywindows').
For more information, tell us which operating system you are using.

you need to do mex -setup to configure the compiler.After which, you run the make.m file in the matlab folder.. Please read the README file.. the details are there...

Related

mexw64 type file in Matlab

I download some mexw64. type files with some Matlab .m files. I want to ask how can I open them and investigate the code and algorithm through them?
Is it possible to convert them to the .m file?
(the windows in my laptop is 64 bit)
Unfortunately not, unless you can contact the author of the MEX file.
MEX files are Matlab executable files. A MEX file is first written in C/C++/Fortran, and then compiled to a binary file with extension .mexw64 (Windows), .mexmaci64 (MacOS), or .mexa64 (Linux). The extension is chosen by the author, based on whether he/she intends the file to be used on Windows, Mac, or Linux.
See the documentation for more information, and the Matlab Q&A site for possible indirect solutions.

Using OpenCV with Matlab: mex does not find header files

I am trying to connect Matlab and OpenCV following this tutorial: http://xanthippi.ceid.upatras.gr/people/evangelidis/matlab_opencv/
Since I work on a Linux system I can't follow the instructions for the mexopts.bat file since the Linux equivalent (mexopts.sh) seems to be rather different and i find none of the options mentioned in the tutorial in the mexopts.sh file.
So I try to set the options in Matlab.
I downloaded the most recent OpenCV Version (2.4.8) and compiled it according to the instructions on their site (http://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html without the make install).
The structure of the OpenCV directory seems to be a problem, since there are multiple include directories and I was unsure which to specify.
[edit]:
there was a lot of pointless code here which has all been made superfluous as #Peter made me aware that I simply misunderstood how make/make install worked.
"Without the make install" is the problem. The include files and built libraries are scattered all over the source tree, as determined by the build system. make install collects all the headers that are appropriate for use by users of the library and puts them in one directory for including. It does the same with the libraries themselves. make install may also "strip" the libraries, which drastically reduces the size and improves the load time.
If you don't want the installation in a system directory, you can set the install path to be somewhere in your home directory.

Linking MATLAB to a DLL library

I am trying to execute some example code from a MATLAB toolkit, 'oscmex'. This toolkit allows for communication using the OSC protocol over MATLAB. I presume this question is non-specific; it should apply to any toolkit that is set-up in the manner that this one is.
Reasons aside, I'm having some simple trouble getting the toolkit up and running. The toolkit comes with no documentation whatsoever; just a set of six DLL files (in one directory), and a set of four MATLAB '.m' example code files (in another directory). Every toolkit I've used in the past has either been a built-in kit or has had an intuitive (semi-automated) install procedure.
After downloading the toolkit, the first thing I tried was to simply run one of the '.M' example codes. This failed as the first line of the code contained the function osc(), which is not (currently) recognised by MATLAB.
So, I figured maybe I need to move the '.M' files into the same folder as the DLLs; perhaps MATLAB would see the functions inside the DLLs. No dice.
So, I realise that I have to somehow link MATLAB to the DLLs on startup. I tried adding the DLLs to a folder and adding an entry to that in the 'pathdef.m' file. This also failed.
I've read somewhere I can load a DLL file by using the loadlibrary() function. So, I tried doing this for the DLL files. This failed on the first file:
>> loadlibrary('osc_free_address.dll')
Error using loadlibrary>lFullPath (line 587)
Could not find file osc_free_address.h.
I'm starting to run out of options... How can I get this set of DLLs up and running?
Browsing this library's web page it would seems these DLLs are just old form of mex files.
Therefore, they should not be used in the context of shared library (e.g., using loadlibrary and calllib), but rather compiled directly to mex files.
To do so, I would suggest the following steps:
Make sure you have a working mex compiler configured for your Matlab.
In matlab, type:
>> mex -setup
this will guide you through the configuration process. I understand that you are working on a windows machine, I usually work with visual studio compiler - works best for me.
This library's README file suggests that OSC
requires liblo 0.22 or later. See http://plugin.org.uk/liblo/
Make sure you have this library and it is located in you LD_LIBRARY_PATH (see e.g., this question for details, or the mex docs).
Get the source code for OSC library from their repository.
Compile the sources in matlab using
>> mex -O -largeArrayDims osc_free_address.c
>> mex -O -largeArrayDims osc_free_server.c
and so on for all 7 c source files. After mex-ing the c files you'll have mex files that you can run from Matlab as if they were regular functions.
You may find it useful to use the library's make file, as suggested by Andrew Mao.
Good luck,
If you look at the build for that software, it is compiling mex files, not DLLs (shared libraries): http://sourceforge.net/p/oscmex/code/4/tree/trunk/src/osc_make.m.
I would try using the mex commands instead of the dll commands (perhaps the files are just misnamed.) Even better, I would compile the files yourself with mex using the build file in source.
Note that the instructions also say that you need liblo-0.22 in order to run the library, so make sure you have that accessible as well.
I took a look at your OSC Toolkit. It seems they have been compiled by MATLAB mex. But, it is not mentioned for which kind of architecture they have been built. You can type mexext at MATLAB command prompt to find the extension for your MATLAB mex files. Then, change the DLL extensions to the given extension. If the original mex is compatible with your matlab, the DLL can be easily accessed by MATLAB. Just make sure to add the folder to your MATLAB path.
Try changing the extension from .dll to .mexw32 (in win32), or .wexw64 (in win64). It's a long shot but it might work.
The Shared Libraries cannot be used directly. As you have mentioned, you need to load them into MATLAB using loadlibrary. According to the documentation, loadlibrary takes two arguments (at least). The first argument is the name of the file, and the second one is the header file which contains definition of functions and external variables. If you do not provide the header file, the MATLAB looks for the a file with the same name as the DLL. Having said that, you need to have access to the header file or at least if you know how the function looks like, you need to write a header for the DLL.
I have worked with the DLLs in MATLAB. The MATLAB is not very user-friendly as long as DLL is concerned. Especially, if the DLL is written in a language other than C (or C++) you will have trouble loading the function into MATLAB.
Besides, MATLAB can only support some specific DLLs. Based, on your version of MATLAB, you need to find out whether or not the shared library is supported by MATLAB. Have a look at Here
In a nutshell, it is not easy to load a DLL into MATLAB. You need to have some information from DLL.

building MATLAB jpeg toolbox with libjpeg8d

This question is related to another question I asked here:
Error reading image using jpeg_read from Matlab's jpeg toolbox
I've been trying to compile the jpeg toolbox under Windows 7 (using the commands Shai provided in the answer to the question I posted), but I get the following error:
jpeg_read.c(52) : fatal error C1083: Cannot open include file: 'jerror.h': No such file or directory
which I believe happened because I haven't built libjpeg. I tried to build libjpeg6b like jpegtoolbox's README says, but I couldn't find a clear guide on how to do it on Windows with visual studio 2010 (and libjpeg's install document doesn't help much), so I ended building libjpeg8d.
My question is if there's any way to use libjpeg8d to compile the jpeg toolbox. I've tried running the command:
mex -I<IJGPATH> jpeg_read.c <LIBJPEG>
with IJGPATH being my libjpeg8d installation folder and LIBJPEG being the path to the jpeg.lib file, inside IJGPATH/Release, but I still get the same missing jerror.h error as before.
Thanks in advance.
Jpeg toolbox contains Matlab routines for manipulating JPEG files. While Matlab's built-in IMREAD and IMWRITE functions provide basic conversion between JPEG files and image arrays, they do not provide access to the details of the JPEG image, such as the JPEG coefficients or the quantization tables.
The routines in this package provide additional functionality for directly accessing the contents of JPEG files from Matlab, including the Discrete Cosine Transform (DCT) coefficients, quantization tables, Huffman coding tables, color space information, and comment markers. The toolbox can be added to Matlab to use the functions.
First check whether the following are installed in your system.
1.Microsoft windows sdk7
2.Microsoft visual c++ 2010 express
if they are not installed, Download and install them in the order specified.
Note: Before installing windows sdk, uninstall any redistributalbe packages of visual c++ 2010. Else windows sdk produces problem during installation and the install fails. During installation dont forget to check the 'x64 Libraries' for 64bit OS and 'x86 Libraries' for 32bit OS, under Windows Headers and Libraries. This allows to use compiler tools for 64 bit operating systems. Once it overs, install visual c++. These tools are free and available at microsoft website. Both online and offline installers are available.
Now comes the real integration process
Step 1: Download the jpeg toolbox and extract it to a separate folder (eg. jpeg)
Download jpeg toolbox
Step 2: Download jpeg source files and extract it to a separate folder (eg.jpegsrc). In the folder jpeg-6b which is in jpegsrc, rename jconfig.vc to jconfig.h and makefile.vc to makefile
Download jpegsrc file
Step 3: From start->All programs-> Microsoft windows sdk , open the command prompt. This opens the 'windows x64 debug environment'. Navigate to the jpeg-6b folder which is inside the jpegsrc folder which is extracted at step 2. Run the command 'nmake clean all' without quotes. This creates the libjpeg.lib file in the same folder.
Step 4: Now open the matlab and choose the workfolder as jpeg, created in step 1.
Now in the command windows run the following commands,
mex -I jpeg_read.c
mex -I jpeg_write.c
Replace with the path to the IJG jpeg-6b directory created in step 2, and
with the full path to the IJG code library file (libjpeg.lib).
To use the jpeg_read and jpeg_write functions copy jpeg_read.mexw64 and jpeg_write.mexw64 files created above to your work directory. Dont uninstall visual c++ compiler or it wont work.
If you are trying to work in Windows, you need to rename jerror.vc to jerror.h
Also, when you mex the files, you need to edit the jpeg_read.c and jpeg_write.c from include to include "jerror.h"

Matlab deployment: add files to source control?

I've created a deployment project which works rather well and now I want to add it to source control repository for others to use.
The main problem I'm facing is that the .prj file which deploytool creates contains absolute paths which will not work on other computers. So far I've tried the following:
Create the stand alone exe using just mcc without deploytool. This works great but I could find a way to create the final _pkg.exe which contains everything. mcc doesn't seem to be able to create this file and there doesn't seem to be any other tool which does. Is this really the case?
Edit the .prj file to include relative paths instead of absolute paths. This only works partially because the .prj file contains a section called MATLABPath which is always replaced with the current setpath of matlab. anyone which uses this file will have to check it out since it is being changed when used.
Find a way to generate the .prj file. the mcc documentation say: Project files created using either mcc or deploytool are eligible to use this option. suggesting there is a way to create a .prj file using mcc but I wasn't able to find how this can be done.
Is there a solution to this situation?
We ran into the same thing with Matlab Builder JA. Not only are the paths absolute, Matlab also adds other toolboxes that it finds on the path, which is irritating, as the build machine doesn't have licenses for a lot of them.
So what we do is:
Edit the prj file by hand to get rid of the absolute paths
Check it into source control and build with mcc
NEVER EVER check in the prj file after it has been touched by deploytool (do all changes by hand in an editor)
Our revision control lets you modify files without an explicit checkout, so using deploytool is not a problem. But why would you want to?
Not sure what the final packager does, but if it just bundles the MCR with the compiled binary, I would just write a replacement.
I personally use Visual Studio 2005 project to maintain my deployment projects and then convert the VCPROJ to PRJ on the fly using build command step
http://younsi.blogspot.com/2011/11/matlab-relative-path-issue-in-prj.html
Here's the mcc option documentation.
What I've found most useful is creating a standalone exe using mcc:
mcc -C -m <function.m> -a <fig> -a <dll> -a <etc> -d <outputPath>
The -C option tells mcc to generate the ctf file (which is the archive of all the compiled MATLAB stuff) as a separate file. I've had trouble on some target computers using a single exe with everything compiled in.
The -m option tells mcc to make an exe.
The -a options tell mcc to include the specified file in the package. For instance, if your script uses two fig files and a data file, you need a -a for each to make sure they get put in the output package.
To tell mcc to use a project file (I've never done this myself):
mcc -F <projectfile>
Also, if you're using R2009a on Windows, there's a known bug that requires some manifest manipulation.