Showing posts with label Matlab. Show all posts
Showing posts with label Matlab. Show all posts

Friday, January 14, 2011

Compiling C/C++ code from Matlab on Ubuntu Linux machine

It seems that there is much easier way of compiling and running C/C++ from your Matlab application, than the one I was trying before.

Reproducing the instructions on this blog here:

My version of gcc (4.4) is too high for Matlab R2009b (4.3), and Matlab chokes on it. The solution is as follows:

Install gcc-4.3:

sudo apt-get install gcc-4.3
sudo apt-get install g++-4.3

In matlab, run mex -setup and select the GCC compiler.(Choose option 2). Next, at the command line:

sudo gedit ~/.matlab/R2009b/mexopts.sh

Replace all instances of CC=’gcc’ with CC=’gcc-4.3′, and al instances of CXX='g++' with CXX='g++-4.3'.
And restart Matlab !

Compile gcc from souce on Ubuntu 10.04.1 LTS

To run C/C++ code using the mex utility of Matlab 7.11.0 (R2010b I needed to compile a different version of GCC than what was already installed on my machine. (I had the gcc version 4.4.1-4ubuntu9 and the currently supported version with mex is 4.3.4)

So I downloaded gcc-4.3 release from here.
Untarred the file: tar -jxvf gcc-4.3.4.tar.bz2

And now when I tried to use the ./configure from inside the gcc-4.3.4 folder, the system complained that it could not find two necessary API's namely GMP and MPFR.

The issue can be resolved by doing the following. We need to (1) download the GMP and MPFR libraries separately , (2) place them inside the GCC folder and then, (3) tell the configure script to look for it there.
tar -jxf gcc-4.3.2.tar.bz2
tar -jxf mpfr-2.3.2.tar.bz2
mv mpfr-2.3.2 gcc-4.3.2/mpfr
tar -jxf gmp-4.2.4.tar.bz2
mv gmp-4.2.4 gcc-4.3.2/gmp
cd gcc-4.3.2
./configure --with-gmp-include=$(pwd)/gmp --with-gmp-lib=$(pwd)/gmp/.libs
make
make install

Hopefully, that should solve the issues !

Wednesday, June 4, 2008

MATLAB vs. C++? And the winner is….

….C++, of course! And, for the simple reason that MATLAB IS EVIL!

To all you pseudo-programmers, Simulink-flaunting ‘engineers’ and the like who swear by the ‘inbuilt functionality’ and ‘user-friendliness’ of MATLAB, I would just like to say… people, get a life! Its not that you don’t KNOW how stupid this application is- after all, you are the ones who sit there wiggling your toes while MATLAB takes minutes to run something C++ would do in micro-seconds; and you are the ones who get stymied, trying to make MATLAB understand even most simple Data Structures - then why not accept it? Why not, for once and for all, agree to the fact that C++ is superior and stop going ya-ya-ya about the ‘cool’ things about MATLAB!

People often tell me that MATLAB works better for engineers because of its inbuilt libraries and as a proof (by demonstration) they may type ‘14382^11.32’ on their command windows and lo-and-behold MATLAB tells you the answer! And then their argument may go something to the effect that to do the same thing with C++, you would need to write a function, maybe include a couple of libraries, debug, compile and run … too much headache for something MATLAB does in ONE line! Well, that is a fair argument…. only if you think that that is engineering… namely calculating random numbers to the power of random numbers…!

Honestly, that is anything but engineering! Engineering is about conceptualizing and it takes power to design elegant structures to translate those concepts to code… A good data structure is the difference between an understandable, well-organized, effectual program and useless, exasperating gobbledygook. But that is something MATLAB just does not understand. The ‘arrays’, ‘cell arrays’ and ‘structure’ that it provides address the problem of stucturing data with the same success rate as Bush is addressing the issue of ending the Iraq War!

Engineering also needs checks and while I admit that C/C++ has its own shortcomings when it comes to security issues (memory leaks, garbage collection problems, etc.), MATLAB is just outright scary. For example, there are no type-checks as a variable can be redefined from an integer to a character, to an array, to a structure within the span of a single function! Further, MATLAB passes parameters only by value… which is totally sinful if you want to pass large arrays and structures… just think of how much time and space you are consuming!

With C++ and a little more patience you can write code that is not only more efficient, it also better structured, more-understandable at any later date and therefore more reusable… I guess that’s why the latest (2008a) version of MATLAB had introduced object-oriented concepts and parameter passing by reference… But that’s something I am yet to get my hands dirty with….