Wednesday, 19 January 2011

Inline comments in LaTeX

I recently got some feedback on an article that I've submitted, and when I started including the reviewer's comments in the revised version, I started wondering if there is a way to make an "in-line" comment in LaTeX documents. I often use the percent sign, %, to comment out everything after a line, like this:

... and thus we see that these methods are equvivalent. % Or are they?

After some searching I found an interesting post on Eric Rasmussen's blog, and it seems that the easiest way to do this is to define a "macro" that doesn't do anything, in the document preamble:

\newcommand{\comment}[1]{}

and then I can make an inline comment using the \comment{} command, like this:

... these methods are equvalent. \comment{Or are they?} Luckily, it doesn't matter...

Thursday, 13 January 2011

Compressing Matlab AVI videos in Linux

Recently I've started making a few movies in Matlab. This is actually quite easy - for an example see this description from the Matlab documentation. Unfortunately, the movie2avi() function does not apply any compression when writing the movie to file (not in Linux, anyway), and this results in very large movies (on the order of 100 MB for a few seconds).

However, I found a solution. I originally posted it as a reply in the MATLAB Newsreader, but I thought I'd share it here as well:

You can transcode Matlabs AVI movies using FFmpeg (www.ffmpeg.org). If you don't have the package, you can install it using the terminal:

sudo apt-get install ffmpeg

If you have only one file, let's call it inputFile, you can transcode it using something like this:

ffmpeg -i inputFile.avi -sameq outputFile.avi

The "-sameq" option is used to preserve video quality. I tested this on a 80 MB file produced by Matlab, and got a 5 MB file without any visible loss in quality. If you want to do it within your matlab script, use the system() command:

system('ffmpeg -i file1.avi -sameq outputFile.avi')

If you have a problem with the Matlab AVI file growing too big, you can split it into several smaller files and compress them as you go, and finally combine them after your iteration has finished. A way of doing this is described here in the FFmpeg FAQ. Note that you will have to transcode your intermediate files to MPEG format. .

Thursday, 11 November 2010

Printing to pdf from Matlab via eps

I use pdflatex for my publications, both because it is convenient to have PDF as the output format, and also because it accepts many kinds of graphic files, like JPG and PNG. If you want to use a vector graphics format, then you can save your graphics as PDFs. This is quite easy to do in Matlab, but I have found that it often leads to rasterization artifacts in the graphics. Exporting to EPS seems to work a lot better in this regard. The solution: Exporting from Matlab in EPS format, and postconverting to PDF using the esptopdf function (part of the texlive-extra-utils). I have made a Matlab function to automate the process:

function printpdfviaeps(filename, varargin)
%PRINTPDFVIAEPS Print current figure to pdf via eps
%
% printpdfviaeps(filename) will save the current figure to filename.pdf,
% using a default paper size of width = 12 cm, height = 9 cm.
%
% printpdfviaeps(filename, width, height) lets you specify the width and
% height yourself.
%
% Martin Skjelvareid, 2010-11-11

width = 12; % Default width in centimeters
height = 9; % Default height in centimeters

if nargin > 1
width = varargin{1};
end
if nargin > 2
height = varargin{2};
end

set(gcf, 'PaperUnits', 'centimeters');
set(gcf, 'PaperSize', [width height]);
set(gcf, 'PaperPositionMode', 'manual');
set(gcf, 'PaperPosition', [0 0 width height]);

print('-deps2','-r150', filename)
system(['epstopdf ' filename '.eps']);
delete([filename '.eps'])

Monday, 8 November 2010

Resetting ssh server host key

Today I tried logging on to a server that was recently down due to a brute force attack. Using the SSH command resulted in the following warning:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.

[...]

In this case, the problem was that the RSA host key had changed. After searching the net for a while, I found that some people routinely delete their known_hosts file because of this. However, there is a proper solution to the problem, as presemted by *ccm* in this blog post:

ssh-keygen -R

where is replaced with the name of the server that you're trying to connect to.

Thursday, 7 October 2010

Checking which memory banks are used on Linux

Today, I decided that I want to upgrade from 2 GB to 4 BG memory on my laptop. However, I didn't know how this memory was distributed - was it two 1GB chips, or a single 2 GB chip? Luckily, a colleague of mine found this command:

sudo lshw -c memory

which, among other things, produced these lines:

*-memory
description: System Memory
physical id: 2b
slot: System board or motherboard
size: 2GiB
*-bank:0
description: SODIMM DDR2 Synchronous 667 MHz (1.5 ns)
physical id: 0
slot: DIMM 1
size: 2GiB
width: 64 bits
clock: 667MHz (1.5ns)
*-bank:1
description: SODIMM DDR2 Synchronous 667 MHz (1.5 ns) [empty]
physical id: 1
slot: DIMM 2
clock: 667MHz (1.5ns)


Voila! From this info it's easy to see that memory bank 1 has 2 GB of memory, and bank 2 is empty.

Thursday, 30 September 2010

Solving installation problems with Comsol 4.0a in Ubuntu Linux

Today I decided to install Comsol Multiphysics 4.0a, which is a physics simulation program based on the Finite Element Method (FEM). I'm running Ubuntu Linux 9.10 (Karmic Koala), and the installation procedure should be as simple as this (quoting from the installation manual):

"To start the installation, type the command

sh {drive path}/setup

where {drive path} refers to the mount point of the DVD-ROM
drive on your system, for example, /media/cdrom."

When I insterted the Comsol DVD, it was mounted as "COMSOL40a ". Note that there are some trailing white spaces which don't make any sense. When I tried to run the installation program, I got an error message saying that "/media/COMSOL40a" did not exist. Turns out that the installation program was looking for a mount point directory without any white spaces.

My solution to this was to mount the CD to another folder, to a folder name without trailing white spaces. I didn't know how do this, but I found a simple description at this web site. In the terminal, I typed

sudo mount -t auto /dev/cdrom /media/tmpcd/

to mount the CD to /media/tmpcd. After that, I could run the installation program as instructed, by typing

sh /media/tmpcd/setup

I'm not sure who is to blame for this "bug", Ubuntu or Comsol, but at least I figured out how to work around it. :-)

Tuesday, 28 September 2010

Solving Ubuntu full disk problem related to SBackup

OK, so today I came to my office finding that somehow my Wubi Ubuntu installation had somehow filled up completely. There was absolutely no disk space left, and because of this, the whole system was so slow that it was unusable. Not really realizing how this could have happened, I tried to reboot. The log-in screen now looked different, and there was a warning about power management not being set correctly. When I tried to log in, the screen only blinked, before the log-in screen appeared again.

Getting pretty frustrated at this point, I started looking through forum threads for posts on similar problems. I found several tips on cleaning up a full partition, all of which involved getting into "terminal mode" by pressing Ctrl-Alt-F1 at the login screen. This made me able to browse through different folders, and check the disk usage using the command du, but I still couldn't figure out why the disk had filled up.

Eventually, I found this forum thread which suggested that the backup program that I use, Simple Backup (or SBackup) may have been saving backup files on my laptop's internal hard drive rather than the intended external drive. A blog post by rvdavid confirmed that several people have had this issue.

My external drive is called "Martins Backup", and when it is connected, it is usually mounted to /media/Martins Backup. It turns out that the backup program kept saving files to /media/Martins Backup EVEN WHEN IT WAS NOT CONNECTED (sorry about that), effectively storing them to the internal drive. Investigating this further, I found 20 GB worth of backup files clogging up the system. Luckily, they are all gone now, my Ubuntu system works as usual, and I'm changing my backup program as soon as I find a suitable alternative. Puh! :-)