Tuesday, 8 December 2020

Save a Matlab movie to an AVI file:

 Simple example script to save a Matlab movie to an AVI file:

Z = peaks;

surf(Z); 

axis tight manual 

set(gca,'nextplot','replacechildren'); 


v = VideoWriter('peaks.avi');

open(v);


for k = 1:20 

   surf(sin(2*pi*k/20)*Z,Z)

   frame = getframe(gcf);

   writeVideo(v,frame);

end


close(v);

Tuesday, 18 December 2012

Installing TU/e-fonts for use in LaTeX

The TU/e-fonts (so-called huisstijl in Dutch) are available from https://www.dropbox.com/s/9hn9a2991ppvavn/latex_huisstijl.exe. To use them in LaTeX:
  • unzip the folder in your local LaTeX-folder (usually ~/texmf)
  • switch to the new subfolder dvips/config
  • run the command
       updmap --enable Map=tue.map

Thursday, 6 March 2008

Movies in Matlab

This script shows the very basics on how to generate a movie in Matlab

% Generate a simple movie: plot f(t, x) = sin(x - t) on (0, 2pi) for different t

for i = 1:25

% Make a figure
t = 0.1 * i;
fplot(@(x) sin(x - t), [0 2*pi])

% Store the figure as a frame in the movie
M(i)=getframe;

end

% Play the movie ten times
movie(M,10)

Friday, 1 February 2008

Change Matlab-figures for print

If you use Matlab to generate EPS-files for inclusion in LaTeX-documents, you'll soon find that lines are too thin and text is too small. I use the following script to increase thickness and size in the current figure in Matlab:
linewidth = 2;
fontsize = 16;
fig = gcf;
h = findall(fig, 'Type', 'Text');
set(h, 'FontSize', fontsize)
h = findall(fig, 'Type', 'Axes');
set(h, 'FontSize', fontsize)
h = findall(fig, 'Type', 'Line');
set(h, 'LineWidth', linewidth)

Source code in LaTeX

If you want to include source code in LaTeX, you can of course use the standard verbatim environment. There are however many advantages to refer to the actual source file on disk instead of copying its contents into your LaTeX document. Referring to a file on disk can be achieved by using the moreverb package.

I use the following commands:
\usepackage{moreverb}\def\verbatimtabsize{4\relax} 
to include the package and to set the amount of white space for tab characters to 4 (default is 8). Next define
\newcommand{\listscript}[1]{{\textbf{#1}\footnotesize\verbatimtabinput{#1}}}
to have a new command for including e.g. Matlab scripts. Use it as
\listscript{cp19.m}
to have the file cp19.m in your document.

Thursday, 1 November 2007

Passwordless ssh

It is very convenient to use ssh to login into different workstations or for instance our Beowulf cluster elegast without having to re-type your password all the time. It is possible to setup things in such a way that this is possible. Here's the howto.

First you need to generate a so-called RSA key. Type
ssh-keygen -t rsa

This will create two files: id_rsa and id_rsa.pub. The first should be kept secret, the second is public. You should now login into the remote machine. If the file ~/.ssh/authorized_keys exists, append the contents from the file id_rsa.pub to it. If it does not exist, create the directory ~/.ssh (with permissions rwx for yourself and no permissions for group and others) and the file authorized_keys (with permissions rw- for yourself and r-- for group and others) with as its contents the file id_rsa.pub.

You should now be able to do passwordless ssh logins.

Bash programming

The standard shell under Linux is called Bash (the GNU Bourne Again SHell). Even though people are generally used to do a lot of stuff using graphical user interfaces, the command prompt can be very convenient for some tasks. Here are a few simple examples of how to get things done using your keyboard rather than your mouse.

Command line use by example

To convert all .gif files in a directory to .jpg, type
for file in *.gif; do convert ${file} ${file%gif}png; done

To find all .tex files in a directory including the subdirectories, type
find . -name "*.tex"

To filter out those files that contain the word "dimension", use
grep dimension `find . -name "*.tex"`

The code between the backquotes (`) is evaluated first and the output it generated is listed as if you typed it at the end of the command. In Bash you can also use $(command) as an alternative to the backquotes, which has the benefit that it can be nested. For example to get the number of bytes of all tex-files that contain the word dimension, you can say
du -s -k $(grep -l dimension $(find . -name "*.tex"))

To make the search on "dimension" case insensitive, use
grep -i dimension `find . -name "*.tex"`

To count the number of occurences, use
grep -i dimension `find . -name "*.tex"` | wc -l

which might be written shorter - but less illustrative - as
grep -c -i dimension `find . -name "*.tex"`

I'm sure you can use these examples to do powerful text searches yourself.

To rename a bunch of .gif files in a directory to new names like 001.gif, 002.gif etc., you could type

l=1
for f in *.gif; do
mv $f $(printf '%03d' $l).gif
l=$((l+1))
done

This example also shows how to use if statements. A simple for loop can be programmed as
for (( n = 1 ; n < 9 ; n++ )); do echo $n; done


Writing scripts

If you have many commands that you use more than once, you can also create a text file in your favourite editor, type #!/bin/bash as the first line, and then put some shell commands. If you save the file and make it executable with chmod (see below), you have written a shell script and can simply execute all command by typing the script's name. This is like creating a Matlab script versus typing commands in the Matlab command window. In bash scripts, you can use a couple of funny looking variable names that have special meaning:








Variable Meaning
$0 name of the script
$# number of arguments given on the command-line when calling the script
$1 first argument given to the script
$* all arguments
$@ all arguments
$$ PID of the script

The difference between $* and $@ is subtle. Usually the latter does what you think it will do, while the first one has a slightly different semantic meaning. If you say "$*" it will expand to the single string "$1$IFS$2$IFS$3...", where IFS is an environment variable that contains a space by default but might have another value, e.g. the character ':'. When you say "$@" it will expand to the list of strings "$1" "$2" "$3"..., which is usually what you mean.