Friday, 27 March 2015

Mac: How to ignore files with errors when copying

When copying files on a Mac using Finder, the copy operation will be interrupted if a file with errors is detected, which makes sense in many cases. However, in some cases you just want to copy "what you can get", and ignore invalid files. This can be done using the "cp" command in terminal, as described here.

Use the following syntax in terminal:

cp -Rp SourceFolder DestinationFolder

The -R flag will ensure that the copy operation is continued even if there are errors. The destination folder does not have to exist, you can specify a new folder name and it will be created.


Wednesday, 2 July 2014

Exporting report number and institution from Mendeley to Word

I currently work for a research institution which produces a lot of reports, and I have struggled to get proper citations for these reports when using the Mendeley Word plugin. If you use the "report" type in Mendeley, you're able to enter institution and issue, but these are not exported to Word. This is my workaround: Use the "journal article" type when entering the report metadata, fill in the report number in the "issue" field, and set the journal as "XXX report" (where XXX is the name of the institution).

This may not be strictly correct, but it works for me. Check out the Mendeley citation style editor if you want to do something similar (and do it properly).

Monday, 14 October 2013

Making an animation from a set of images in Powerpoint

If you have a set of images which shows how something changes with time, it is often useful to display them in rapid succesion in a presentation. If you're using LaTeX, I recommend looking into the Beamer class (search for "overlay" in the Beamer manual). However, this post is about PowerPoint.

Let's assume that you have three images, named im1.png, im2.png and im3.png, which are the same size. You want to start the slide by displaying im1, and then "overlay" im2 and im3 as you click the mouse or the right arrow button. One way to do this is to create a slide for im1, copy this twice, and inserting im2 and im3 in the two copies. However, this creates several nearly-identical slides, and you would probably also have to manually resize and position each image in the exact same location. If you have several images, this approach will be very time-consuming.

A better way is to add all the images to the same slide, and to display each one in turn using animations. You then want to add an animation for each image after the first one. For example, you import im1 to your slide, then import im2 and add "appear" as an animation, and repeat the same for im3. Pretty simple! The main annoyance here is that the images should be exactly the same size and in exactly the same position, to get the proper "animation" effect.

Rather than adjusting size and position for each image as you add it, you can first add all the images with animations, and then change size and position for all images simultaneously:

  • Add the images in the order in which they should be displayed. For every image after the first, add an "appear" animation
  • Select all the images by drawing a selection rectangle, right-click and choose "size and position" (or something like that, I currently have a Norwegian version of Powerpoint :)
  • Adjust the position. Since all the images are selected, they are now shifted to the same position (for example zero offset from upper left corner) and are perfectly overlapping. You can reposition the images later by selecting all and dragging them to a new position.
  • Adjust the size so that the images fit nicely on the page
With this approach you can make a 10-image animation in less than a minute. 

Wednesday, 27 February 2013

Merging two videos into a single video with side-by-side view using FFMPEG

I am currently evaluating lots of test videos, recorded at different conditions, and found myself wanting to display them both in a side-by-side view, in a single video. As always when it comes to video, I searched for a solution employing ffmpeg, and found a post on Stack Overflow describing almost exactly what I wanted to do. The post gives a demonstration example with scaling and fading of videos, so I had to simplify it to just

  • Pad the left video with extra space on the right, making space for the right video
  • Overlay the left and right videos, and output them to a single video file

This is the syntax I arrived at after some tweaking (all on a single line in the terminal):

ffmpeg.exe -i LeftInput.VOB -vf "[in] pad=2*iw:ih [left]; movie=RightInput.VOB [right]; 
[left][right] overlay=main_w/2:0 [out]" -b:v 3000k Output.mp4

Since I'm not a ffmpeg wizard, I won't claim to understand all the details of the line above. Hopefully, the syntax is (kind of) self-explanatory. However, note the "-b:v 3000k" parameter, which sets the bitrate of the output video to 3000kbits/second. This has to be adjusted in each individual case. 

Thursday, 24 January 2013

Upgrading to Subversion 1.7 in Ubuntu

Just a quick post this time. I recently had to upgrade the Subversion (SVN) software on my Ubuntu system to version 1.7. This version is (currently) not included in the regular Ubuntu distribution, but I found the solution in an Ubuntu forum thread - simply run this in a terminal:


echo "deb http://opensource.wandisco.com/ubuntu lucid svn17" | sudo tee /etc/apt/sources.list.d/svn.list
sudo wget -q http://opensource.wandisco.com/wandisco-debian.gpg -O- | sudo apt-key add -
sudo apt-get update
sudo apt-get dist-upgrade

Monday, 12 November 2012

Batch cropping white space borders in Matlab figures

Matlab figures have, by default, a white space border around them. This is fine if you want to view them by themselves, but wastes space if you want to add them to a text document, where the white borders are "added" to the document margins. It is possible to adjust the border width in Matlab, but if you already have exported the figures to an image file, it's too late. (Note: When exporting to EPS the border is not included by default).

If you want to remove this white space for a large number of image files in one go, ImageMagick is your friend. For example,

mogrify -trim +repage *.png

will remove the white border of all PNG files in the current directory, using the "smart crop" trim option. Consult the ImageMagick documentation on cropping for further options.

Tuesday, 16 October 2012

Editing Grub boot menu order and changing default OS

I recently decided to try running a dual-boot system with Windows 7 and Ubuntu. I first installed Windows 7, and then installed Ubuntu. And with Ubuntu comes the GRUB boot loader, which pops up when I start the machine, and lets me choose which OS to use. If I don't choose anything, the item at the top of the list is selevted automatically after about 10 seconds.

I want to use Windows 7 by default, but unfortunatly Windows is at number 6 or 7 on the list. If I'm not paying attention when I start the machine, it boots into Ubuntu, and I have to restart it. How can I make Windows 7 the default OS?

The answer lies in reconfiguring the GRUB menu. There are several ways to do this, but I used the grub-customizer GUI app, which seems both very simple and flexible. Install it using the terminal:


sudo add-apt-repository ppa:danielrichter2007/grub-customizer
sudo apt-get update
sudo apt-get install grub-customizer

In the grub-customizer, you can reorder the GRUB menu items, and for example check the option "Default OS: Last used OS". You can read more about the grub-customizer on the How-To Geek Blog.

Wednesday, 29 August 2012

Online LaTeX equation editor

Today I got a great tip from my colleague Vidar: A link to an online LaTeX equation editor at codecogs.com. The equations can be downloaded as both raster and vector graphics files. The editor shows a preview of the equation you're writing in "real time", so it's acually also a nice way to test your LaTeX code while you're writing.

Tuesday, 26 June 2012

Creating a simple nomenclature for a PhD thesis (or similar)

The deadline for my PhD thesis is drawing close, and I'm putting on some finishing touches. One of the things I'm currently working on is the nomenclature - a list of all the mathematical symbols. There are several tools for doing this "properly", like the glossaries and nomencl packages, but they work best if you use them consistently through the entire writing process. It's too late for me to use these, so I have to add the symbols to the nomenclature manually. But what formatting should I use? I tried the tabular environment, but my list soon grew too long for a single page.

Luckily, I came upon this post at the howtotex blog written by Frits Wenneker. He suggests using the longtable environment for the nomenclature - which elegantly breaks the table up to fit several pages. Check out his post if you're looking for something similar.

Tuesday, 12 June 2012

Using accented characters with BibTeX

When writing scientific articles, I often cite authors with accented characters in their names, for example

Pörtzgen, N., Gisolf, D., & Blacquière, G. (2007).
Inverse wave field extrapolation: A different NDI approach to imaging defects.
IEEE Trans. Ultrason., Ferroelectr., Freq. Control , 54 (1), 118–127.

Such accented characters often creates problems when BibTeX is used. The reason for this is that BibTeX uses 8-bit encoding for the .bib file, as explained in this post at StackOverflow, and also in the LyX wiki.

The simple solution to this problem is to write accented characters in the BibTeX file using special symbols, as explained on the BibTeX webpage. For example, ö is written \"{o} and è is written \`{e}. With these special symbols, the BibTeX source code for the citation above is


@ARTICLE{Poertzgen2007,
  author = {Niels P\"{o}rtzgen and Dries Gisolf and Gerrit Blacqui\`{e}re},
  title = {Inverse Wave Field Extrapolation: A Different {NDI} Approach to Imaging Defects},
  journal = {IEEE Trans. Ultrason., Ferroelectr., Freq. Control},
  year = {2007},
  volume = {54},
  pages = {118-127},
  number = {1},
  month = {January}
}

Wednesday, 23 May 2012

Adjusting vertical alignment of subfigures in LaTeX

When using subfigures in Latex, I have sometimes found it annoying how the figures are vertically aligned at the bottom, rather than being centered. An example: The following code

\begin{figure}
\subfigure{\includegraphics[width=67mm]{PipeScannerUnderGround}}
\subfigure{\includegraphics[width=47mm]{CylScanGeometryPipe}}
\caption{}
\end{figure}

produces the following figure:


which seems a little lopsided. I found a nice solution at stackoverflow: Use the \raisebox command to adjust the vertical position of individual subfigures, for example

\begin{figure}
\subfigure{\includegraphics[width=67mm]{PipeScannerUnderGround}}
\subfigure{\raisebox{10mm}{\includegraphics[width=47mm]{CylScanGeometryPipe}}}
\caption{}
\end{figure}

which yields the following result:



Much nicer! :)


Monday, 14 May 2012

Adjusting LaTeX margins to fit a wide figure

I'm currently writing my PhD thesis, trying to shape a number of scientific articles into a coherent whole. In this process, I've come across a problem: The PhD thesis format has a text width of 120mm, which is suitable for one-column text, but I sometimes want my figures to be wider than this. How can I adjust the margins of a page on a per-figure basis? The answer: The changepage package.

Include the package by adding this line to the preamble

\usepackage[strict]{changepage}

... and use the \adjustwidth{leftmargin}{rightmargin} inside the figure environment, for example

\begin{figure}
    \begin{adjustwidth}{-10mm}{-10mm}
        \includegraphics{wide}
        \caption{Wide figure}
    \end{adjustwidth}
\end{figure}

In this example, both the left and right margins are reduced by 10 mm, making more room for the figure. The command also works with subfigures. 

Friday, 4 May 2012

Batch converting PDF to EPS

Today I found myself needing to convert about 20 vector images from PDF to EPS, because a journal that I want to submit an article to, demands that all graphics are in EPS format.

Searching the net, I found several solutions, many of them involving the pdf2ps or pdftops command-line utilities. The best solution was the one I found here, which converts all the images in a folder with a single-line for loop:

for f in *.pdf; do pdftops -eps $f; done

The code is run on the command line. I've only tried this in Linux, but some version of it may work in Windows or OSX as well.

Monday, 20 February 2012

Batch converting images files from one format to another using ImageMagick

Let's say you have 500 JPG images and want to convert them all to PNG in one go. The mogrify function in the ImageMagick software suite comes in handy here:

mogrify -format png *.jpg

This produces a copy of each image in the new format. It's possible to combine the conversion with other operations, for example resizing and conversion to monochrome:

mogrify -resize 50% -monochrome -format png *.jpg

I often create PDF vector graphics which I use together with PDFLaTeX. However, sometimes I need bitmap versions of the vector graphics images. Mogrify can convert from PDF to bitmap formats (for example PNG), but it's important to set the density of the sampling grid in the conversion (dots per inch), otherwise the bitmap image might have a too low resolution. Example:

mogrify -format png -density 600 *.pdf

The commands above can be executed directly in the terminal if you are inside the folder containing the images. The folder can also be specified explicitly, for example

mogrify -format png /home/martin/images/*.jpg

Wednesday, 4 January 2012

Running a graphical application remotely using SSH

This finding is in no way revolutionary, but I found it useful, so maybe others will too. I'm running Ubuntu Linux on my laptop, and recently I wanted to run a Matlab script on a Linux workstation. I can log on the the workstation using ssh, for example

ssh martin@server.name.no

which gives me access to the terminal. However, SSH can also be used for graphical programs, if the server is running a X window system. The SSH connection is configured for X session forwarding by adding a "-X" switch at logon,

ssh -X martin@server.name.no

Such forwarding is further explained here. If I logon using the above syntax and start matlab, it appears as a regular window on my desktop, although it's running on the workstation. Unfortunately it can be a bit slow in responding to mouse and keyboard input, so using the terminal version of Matlab may in some cases be just as good.

Sunday, 11 December 2011

Changing the bit depth of images using ImageMagick

Today I wanted to create a simple black-and-white version of a grayscale image that I have. This is pretty easily done in Matlab (you can scale the image to values between 0 and 1 and use round()), but I was guessing there was probably already some simple way to do it in ImageMagick - and there is!

This is the original version of the image (from the great graphical novel series "Bone"):


To convert it to a 3-bit image (8 levels), I used the -depth switch with the convert command:

convert bone.jpg -colorspace gray -depth 3 bone3bit.jpg


And similarly, for black-and-white (1 bit image):

convert bone.jpg -colorspace gray -depth 1 bone1bit.jpg






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.