Thursday, April 25, 2013

Making MEX in MATLAB R2008a to use Visual Studio 2012

I'm building levmar on my Windows machine and it builds well with Visual C++ 2012 using the existing Makefile that is bundled with levmar package. But I have some problems building the Mex files of Matlab interface for levmar. The problem seems to be because I'm building it with the old lcc compiler, and my Matlab is R2008a, which is pretty outdated.

Unfortunately the latest Microsoft compiler that Matlab R2008a can support is Microsoft Visual C++ 2008. Therefore, I'm looking for a way to make my MATLAB works with Visual C++ 2012. And I found the following steps. Hope this is useful for other people as well.

1. Run mex -setup in MATLAB R2008a.
When the prompt "Would you like mex to locate installed compilers [y]/n? " appears, say No, and select the latest Visual C++ version in the list. Mine is Microsoft Visual C++ 2008.

The purpose in the above step is to let Matlab copy the mexopts.bat into %Appdata%\MathWorks\MATLAB\R2008a folder so we can make modifications on it to make Visual C++ 2012 recognized by Matlab.

2. Navigate to %Appdata%\MathWorks\MATLAB\R2008a. Locate mexopts.bat and open it using Notepad or Notepad++.
For example, on my machine it is C:\Users\Son\AppData\Roaming\MathWorks\MATLAB\R2008a.

3. Look for VS90COMNTOOLS and modify it into VS110COMNTOOLS. This is an environment variable that specifies the installation folder of Visual Studio 2012 (version 11.0).

4. Change the LINKERDIR to
set LINKERDIR=C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A

On your machine you might want to change it accordingly to the Microsoft SDKs folder that contains Bin, Lib, and Include folder, where the Lib folder contains kernel32.lib.

5. Save your mexopts.bat and you are done. Try compiling some Mex files in MATLAB R2008a to make sure it works.

6. Backup your modifed mexopts.bat so next time you accidentally execute mex -setup and the mexopts.bat in the AppData\Roaming\MathWorks folder is overwritten, you have it ready for restore.


Monday, January 28, 2013

Export source code listing to PDF

On Linux, enscript is a tool that can be used to generate source code listing to PDF. For example, I have a C++ source code file named area.cpp and I want to export the source code to PDF format with full syntax highlighting and line numbers.

The following command by enscript can be used:
enscript -Ecpp --color -C -o area.ps area.cpp

Here is the explanation of the above parameters.
-Ecpp: turn on syntax highlighting for C++ style.
--color: export with color fonts.
-o area.ps: save to a PostScript file named area.ps.
-C: include line numbers

To generate PDF, we can simply call
ps2pdf area.ps

Some other interesting options that you can play with:
-G, --fancy-header: add a nice header to each page.
--continuous-page-numbers: no reset of page numbers for each source file.