Friday, 11 November 2011

Setting up Matlab mex compilation in Ubuntu


NOTE: This post was updated on January 16, 2012.

Recently I have been trying to compile some Matlab mex-files from the excellent Machine Vision Toolbox by Peter Corke. When I first tried to compile, I got the following warning:

Warning: You are using gcc version "4.6.1-9ubuntu3)".  The version
         currently supported with MEX is "4.3.4".

I tried several ways of "downgrading" gcc, but as it turns out, the newer version of gcc on Ubuntu 11.10 is backwards compatible and works fine. The message above is simply a warning, not an error, and there is no need to change gcc to an older version. However, my real problem was this: I got errors of the type

error: expected expression before ‘/’ token

I had no idea why this error was popping up, until I read a post on similar issues on xcorr. It turns out that C++ style "//" comments are not allowed in the standard mex configuration that is set when you run "mex -setup". To avoid the error, open the mexopts.sh file (mine is located in ~/.matlab/R2011b/mexopts.sh), and replace all occurences of "-ansi" with "-std=c99".

Thursday, 3 November 2011

Batch renaming files in Ubuntu Linux

I often deal with a large number of data files, for example files containing ultrasound data from experiments. Recently I had to rename about 200 files which were named something like this,

oldName001.mat, oldName002.mat, ..., oldName200.mat

to something like this:

newName001.mat, newName002.mat, ..., newName200.mat

Basically, I wanted to keep the numbering but change the base of the name. I found out that on Ubuntu 11.10, which I'm currently using, there are two different flavors of the "rename" command. One is a Perl script, and using this, the syntax for doing the above operation is

rename "s/oldName/newName/" *mat

To test without actually renaming, use the "-n" switch. Type "man rename" or have a look at these Webmaster Tips for several more examples of this usage. However, there is also a rename command as part of the linux-util package. To distinguish this from the above command in Ubuntu, call the command as rename.ul:

rename.ul oldName newName *mat

Notice that the syntax is simpler and easier to remember for this version. Type "man rename.ul" or have a look at this NixCraft post for more examples. 

Finally I found out that it is also possible to batch rename files using Thunar, which is the default file manager in Ubuntu 11.10. Mark all the files, right-click and choose "rename", and use the "Search and replace" option.

Wednesday, 26 October 2011

Installing Matlab with standalone user on Ubuntu Linux

OK, after doing this in an awkward manner a couple of times, here is how I install Matlab on Ubuntu with a standalone license:

  • Download the files from Mathworks
  • Run the install script with sudo (as root), but DO NOT activate the license yet. Choose custom install, use default install location, tick the box for creating symbolic links.
  • After installing, try to run Matlab (not as root). The activation procedure will start automatically. Register your own user name for the standalone license.
  • Matlab (with GUI) can now be run by calling the command "matlab -desktop". To create a application launcher for it, use the "Main Menu" program (also called Alacarte, can be installed with the Ubuntu Software Center). Get a logo for the launcher here, or put it right where it belongs by using this command:
sudo wget http://upload.wikimedia.org/wikipedia/commons/2/21/Matlab_Logo.png -O /usr/share/icons/matlab.png

I also recommend having a look at the Ubuntu Documentation for Matlab installation. It might not always be completely up to date, but the procedure doesn't change much between releases.

Monday, 10 October 2011

Merging images into movie using FFmpeg

Let's say that you have a number of images, named image001.jpeg, image002.jpeg, image003.jpeg etc., and you want to merge them into a single movie, for example to create a time-lapse video. FFmpeg is a great commend-line tool for doing this kind of stuff. To use FFmpeg to create the movie "outmovie.mp4", with 25 frames per second and reasonable quality, enter

ffmpeg -i image%03d.jpeg -sameq -r 25 outmovie.mp4

Note the format image%03d.jpeg, indicating that the numbering of the files has three digits with (possible) leading zeros. To set the output bitrate explicitly to for example 64 kbits/s, use the switch "-b 64k" rather than "-sameq". See the FFmpeg documentation for lots of other possibilities.

Wednesday, 5 October 2011

Converting images from PDF to PNG using ImageMagick

I usually prefer vector formats like PDF for my figures, but some times I need to make a PNG version. ImageMagick is my tool of choice to do this, since it can be called from the command line. My initial try was something like this:

convert figure.pdf figure.png

which indeed converts the figure from PDF to PNG, but with a much too low resolution in the PNG image. After reading up on the subject on the Magick-users mailing list and the ImageMagick documentation, I found out that the default resolution at which the PDF file is read is 72 DPI. To read with a higher resolution, for example 600 DPI, use the -density option:

convert -density 600 figure.pdf figure.png

As a little side comment, I could mention that I first tried to do this because when I saved a figure from Inkscape as PNG, the resolution was very poor. The workaround was then to save as PDF and convert to PNG. However, afterwards I remembered that Inkscape also has a "export bitmap" option, which will allow you to set the density before saving to PNG. Oh well, the ImageMagick conversion may still come in useful some day...

Thursday, 29 September 2011

Matlab and version control with Subversion

At my company, we use Subversion for version control of our software. I currently use RabbitVCS as a file explorer front-end for Subversion (in Ubuntu Linux), but I've been looking for a way to use Subversion directly from Matlab.

I haven't found any solution that fully integrates with the file explorer in Matlab yet, although there are some useful tools available. However, it wasn't until today that I realized (the bleeding obvious fact) that the Subversion command line interface can be used directly from the command line. Simply put an exclamation mark in front and call the svn commands as usual, for example:

!svn help
!svn status
!svn add file1.m
!svn commit -m "This is a log message"

The command line version of Subversion is both simple and reliable, so until the Mathworks makes an official Subversion plugin, I guess I'm sticking to this approach.

Thursday, 15 September 2011

Using ImageMagick to convert images to grayscale

Although color images are nice, I try to avoid using them in articles, as that always costs extra. Sometimes I also prefer grayscale images for presentations, since the colors of the projector can often be quite different from what I see on my screen.

Anyway, here is a simple way to convert an image from color to grayscale using the command line tool ImageMagick:

convert inputFile -colorspace Gray outputFile