MATLAB no longer supports user-defined MEX configuration? - matlab

I've upgraded my MATLAB to 2014b (on OS X 10.10), and tried to test some old MEX C/C++ codes.
As usual, I run mex -setup from the command window, and I would expect to be provided the chance to overwrite the option file "meshopt.sh".
But now, it seems MATLAB has made some change and I am not allowed to change the MEX configuration, it simply says:
MEX configured to use Xcode with Clang for C language compilation,
...
to choose a different language, select one from the following
mex -setup C++
mex -setup FORTRAN
So, is it true that the old ways to modify "mexopts.sh" are no longer feasible?

It seems that the following command works:
mex -setup my_mexopts.sh
however, there's some warning information,
Legacy MEX infrastructure is provided for compatibility; it will be removed in a future version of MATLAB.
Apart from that, everything looks fine, except I got some warning at compile time seems due to compiler version(gcc 4.9.1) on my current system(OS X 10.10).
gcc: warning: couldn't understand kern.osversion '14.0.0

When I run mex -setup C, I see information like the following:
Microsoft Visual C++ 2008 (C) mex -setup:H:\MATLAB\R2014b\bin\win64\mexopts\msvc2008.xml C
Microsoft Visual C++ 2010 (C) mex -setup:H:\MATLAB\R2014b\bin\win64\mexopts\msvc2010.xml C
Microsoft Visual C++ 2012 (C) mex -setup:H:\MATLAB\R2014b\bin\win64\mexopts\msvc2012.xml C
The mex part is actually a link you can click to choose that configuration. So, to make your own:
Choose an xml as a template and copy it. (e.g I'd copy H:\MATLAB\R2014b\bin\win64\mexopts\msvc2012.xml to %AppData%\MathWorks\MATLAB\R2014b\mex_C++_win64_custom.xml).
Make the edits in the XML file, probably in the <vars> tag (e.g. modify COMPFLAGS or CFLAGS whatever variable has your compiler options) or the <env> tag (e.g. PATH, INCLUDE, LIB, etc.)
Set that to the current configuration:
mex -setup:C:\[YourMATLABAppDataHere]\myCopts.xml C
Note that a good place in Windows to keep settings is under %APPDATA%. For MATLAB R2014b, this would be something like C:\Users\[yourname]\AppData\Roaming\MathWorks\MATLAB\R2014b.
Do the same for C++ or FORTRAN configurations too.

Related

mex file not executing in MATLAB 7.5.0 (R2007b)

I created a .mex file of a MATLAB file test.m from MATLAB R2012b using MATLAB coder.
I got the output file test_mex.mexa64. I was hoping to use this .mex file in MATLAB 7.5.0 (R2007b) but it shows the following error.
Invalid MEX-file '/work/sreekanthl/test_mex.mexa64': libmwblascompat32.so:
cannot open shared object file: No such file or directory.
How can I make this .mex file work in 7.5.0 (R2007b)?
I agree with David Kelley's answer for general MEX files but there is a special consideration for MATLAB Coder generated MEX files. MATLAB Coder generated MEX code is expected to be forward compatible but generally is not expected to be backward compatible. In other words, you are expected to be able to use such MEX code in a newer release than the one with which it was generated, however using the code in an earlier release cannot be guaranteed to work.
The reason for this is that the MEX code uses various runtime libraries which ship with MATLAB. These libraries advance over time and the MEX file from the newer version may depend on features that the older libraries do not provide, or even on a new library which does not exist in the older release as you are seeing here.
If you want to create MEX code that is compatible with earlier releases, you could generate a standalone target such as a static library or a shared library (LIB or DLL respectively for MATLAB Coder), and either:
Try to use loadlibrary to call the generated code in MATLAB
Write a generic MEX interface for it and compile that MEX code in the older release
These options are more work but should enable you to create a MEX file or shared library usable in an older release.
While TMW says that mex functions are usually compatible between versions, it's not officially supported and quite common to run into issues with it.
The only reliable option is to simply recompile the generated source code that Matlab coder created (i.e., the C code) in the version you want to run it in using the mex function.

How could code and compile Matlab mex files over Eclipse?

I want to code and build Matlab mex files on Eclipse IDE. How could I do to include mex compiler as one of the c++/c compilers and build the mex code I have with that compiler on Eclipse?
I don't know what you are trying to say but I'll explain how mex helped me.
Mex is - Matlab Executable this you'd be using to create the functions similar to Matlab functions Ex: function varargout = foo(varargin). This foo will be your foo.c or foo.cpp.
The foo.c or cpp is compiled with the well known C/C++ Compilers mentioned here -http://www.mathworks.in/support/compilers/R2012a/win64.html
This generates a foo.mexw32 or foo.mexw64 in Windows and you can call it like a Matlab Function.
What you can do is write a code in Eclipse using the C compiler it has get it execute and port it to mex functions easily. Select the compilers from mex -setup
I found this tutorial extremely helpful :
Mex Tutorial
and I doubt there is something call "Mex compiler" atleast I've not come across.
I use the tutorial of http://ngoonee.xanga.com/664222870/compiling-mexw32-mex-for-windows-files-using-eclipse-cdt-ide-mingw-gcc-compiler/
Works for me!

using visual C++ compiler in matlab

i installed two compilers in Matlab : LCC and Visual C++ 2008
i want to load a C++ header file in matlab but when i want to do this matlab uses LCC instead of
Visual C++ and gives me some warnings .
i used:
mex -setup
mbuild -setup
but Matlab still uses LCC for loading my C++ header file
>> loadlibrary('DLLing.dll', 'DLLTutorial.h')
Warning: Message from C preprocessor:
lcc preprocessor error: C:\Users\MAHNAZ\Documents\MATLAB\DLLTutorial.h:3 Could not find include file
How can i say to Matlab that use visual c++ compiler instead of LCC??
What version of MatLab are you using? It might not suppose the version of the Microsoft SDK you have installed on your computer. This isn't really an answer but I can't post comments yet.
As per your description, after you type mex -setup, the first thing MATLAB should ask you:
Please choose your compiler for building external interface (MEX) files:
Would you like mex to locate installed compilers [y]/n?
After this, you should press y and then you should see a list of available compilers. Choose a right one from them.
What version of Windows are you working with, is it 32 or 64 bit (question for both Windows and the MATLAB version installed)?
Looking the archived documentation for R2008a (you must be logged in to see the page), it seems that LOADLIBRARY requires VS2005 on 64-bit platforms (perhaps it defaults to LCC if its not available). The truth is it does not explicitly say how it determines what compiler to use. (If you are up to it, you can edit loadlibrary.m and trace the execution to see why it ends up calling LCC instead of Visual C++)
Compare this with the current R2012a help page where it clearly says that you must select the compiler with mex -setup.
Note that LCC compiler will no longer be shipped with future versions.

How to use CMake and Visual Studio 2010 (64 bit) to build a MATLAB R2011a (64 bit) mex file?

I would like to write a CMakeLists.txt such that CMake writes a Visual Studio 2010 (64 bit) solution file to build a mex function for MATLAB R2011a (64 bit) from C++ code example.cxx.
I do not want to use MATLAB's compiler wrapper mex but set up the Visual Studio solution file such that Visual C++ links the relevant MATLAB libraries.
example.cxx has no dependencies except for the MATLAB libraries that are necessary for mex files.
CMake 2.8.7 is set up correctly such that it uses the 64 bit generator for Visual Studio 2010.
The essence of what I am doing right now is
find_package(Matlab)
add_library(example STATIC example.cxx)
target_link_libraries(example ${MATLAB_LIBRARIES})
Neither the compiler nor the linker issues any errors. I install the output example.lib under the name example.mexw64 in a directory in MATLAB's path. When I call example from MATLAB, I get the error message
??? Invalid MEX-file
'C:\...\example.mexw64':
C:\...\example.mexw64 is not a valid Win32 application.
Any suggestions are welcome!
I had the same problem and this link was very helpful (also serves as a nice example of how to use ITK in a MATLAB MEX file btw). I think for your case, you need to add the link flag "/export:mexFunction" to your CMakeLists.txt file. Example below:
PROJECT(example)
FIND_PACKAGE(Matlab REQUIRED)
INCLUDE_DIRECTORIES(${MATLAB_INCLUDE_DIR})
ADD_LIBRARY(example MODULE example.cpp)
ADD_DEFINITIONS(-DMATLAB_MEX_FILE)
# Needed for entry point.
SET_TARGET_PROPERTIES(example
PROPERTIES
LINK_FLAGS "/export:mexFunction"
)
# Change the dll extension to a mex extension.
set_target_properties(example PROPERTIES SUFFIX ".mexw64")
TARGET_LINK_LIBRARIES(example ${MATLAB_LIBRARIES})
Note that the FIND_PACKAGE(Matlab) doesn't seem to work all that well, so that may be a problem for some people. I had to get around it by hard-coding the MATLAB paths into CMakeLists.txt (not shown in this example).
Matlab mex files are dll`s not libs. Try making cmake (not an expert on that) create a dynamic librart، not static.

How to start SVM training on MATLAB

I have a set of facial features that i have obtained and would like to classify using SVM. I intend to use libsvm package and use MATLAB to carry out the training.I have already read up on SVM by watching the Stanford lecture. But I am not sure how to use libsvm to start.. Need some guidance...
In matlab you can link your matlab code with some programs implemented in c. This a called a mex-file.
However you need to compile them, to make it work.
You can read the readme file to know how to compile them:
Installation
On Unix systems, we recommend using GNU g++ as your compiler and type
'make' to build 'svmtrain.mexglx' and 'svmpredict.mexglx'. Note that
we assume your MATLAB is installed in '/usr/local/matlab', if not,
please change MATLABDIR in Makefile.
Example:
linux> make
To use Octave, type 'make octave':
Example: linux> make octave
On Windows systems, pre-built binary files are already in the
directory `..\windows', so no need to conduct installation. Now we
include both 32bit binary files and 64bit binary files, but in future
releases, we will provide binary files only for 64bit MATLAB on
Windows. If you have modified the sources and would like to re-build
the package, type 'mex -setup' in MATLAB to choose a compiler for mex
first. Then type 'make' to start the installation.
Example:
matlab> mex -setup
(ps: MATLAB will show the following messages to setup default compiler.)
Please choose your compiler for building external interface (MEX) files:
Would you like mex to locate installed compilers [y]/n? y
Select a compiler:
1 Microsoft Visual C/C++ version 7.1 in C:\Program Files\Microsoft Visual Studio
[0] None
Compiler: 1
Please verify your choices:
Compiler: Microsoft Visual C/C++ 7.1
Location: C:\Program Files\Microsoft Visual Studio
Are these correct?([y]/n): y
matlab> make
For list of supported/compatible compilers for MATLAB, please check
the following page:
http://www.mathworks.com/support/compilers/current_release/