Matlab / Octave tips
Whenever I write [Octave] it means the stuff also works in Octave
General
Functions
I/O
Debug
Dates and times
Strings
Plot
Surface fitting
General
Entering long statements
use ‘…’
all_data = textscan(fid, ['%n %n.%n.%n-%n.%n %n %s %n %s %d %s %d' ...
%s %d %s']);
Output format [Octave]
format
Most used options:
short
long
rat:
ratio
Variable type
whos myVar
Difference between && and &
&& is a short-circuit version of the & logical operator meaning when entering
a && b
Matlab first evaluates a and if it is wrong, terminates immediately without evaluating b
Example:
if ( (x > 0) && (1/x <3) ) % 1/x will never be evaluated with x = 0
Functions
Making new functions visible
If you’re creating a new function with an editor that is not the Matlab editor, Matlab doesn’t detect it automatically. You solve this problem by typing
clear functions
Of course the function file must be in your path, otherwise you can add it from the File menu
Syntax
function y = average(x)
% AVERAGE Mean of vector elements.
% only first comment line is printed when you type "lookfor average"
% the other lines print when you type "help average"
y = sum(x)/length(x); % Actual computation
function [Xout,Yout] = myFunc(x)
Number of arguments
nargin: number of input arguments
nargout: number of output arguments
Variable number of input arguments
function testvar(x1, x2, varargin)
Varargin is a cell array. In the function body, access its cell values like this:
varargin{i}(k)
Variable number of output arguments
function [varargout] = testvar2(arrayin)
Varargout is a cell array. Assign the cell values like this:
varargout{i} = arrayin(k,:);
Anonymous functions
Does not require an M-file (perfect for defining functions in the command line)
f = @(arglist) expression
example:
sqr = @(x) x.^2;
I/O
Open a formatted text file
fid = fopen('file.dat','w')
w
: write
r
: read
a
: append
returns -1 if error
If the file is a text file and you want to write to it, consider using ‘t’ as an attribute if you want Windows line break
example:
fid = fopen('file.txt', 'wt')
Close file
fclose(fid)
fclose('all') % closes all open files
Filename
[pathstr, name, ext, versn] = fileparts(filein);
fileout = fullfile(pathstr, [name, '.out']);
Write to formatted text file (line by line)
fprintf(fid,'%2.4f\n',x)
fid: 1 = stdout; 2 = stderr
omitting fid writes on the screen
d: integer
f: fixed point notation
e: exponential notation
g: same as f or e but insignificant zeros do not print
s: string of characters
c: character
Remember \n for line break
fprintf tips
Printing strings and numbers together: it looks like you have to separate the printing of strings and numbers into distinct fprintf statements. Example:
fprintf(fid, '%s', 'test ');
fprintf(fid, '%d %d\n', 10, 20); %printed on the same line as 'test
‘
Write (numeric) matrices to text file
A = [1 2 3 4 ; 5 6 7 8];
dlmwrite('my_data.out', A, ';')
‘;’ is the delimiter in my_data.out
” means no delimiter
Note: not necessary to open the file first
Note: use the following notation if you want Windows line break:
dlmwrite('my_data.out', A, 'delimiter', ';', 'newline', 'pc')
To decide the number precision use the ‘precision’ attribute
'precision', '%10.5f
‘
Read ASCII delimited numeric data file
data = dlmread('myfile.dat');
col1 = data(:,1);
Read from file containing numbers and strings
textscan
example:
Text file scan1.dat contains data in the following form:
Sally Level1 12.34 45 1.23e10 inf NaN Yes
Joe Level2 23.54 60 9e19 -inf 0.001 No
Bill Level3 34.90 12 2e5 10 100 No
Read each column into a variable:
fid = fopen('scan1.dat');
C = textscan(fid, '%s %s %f32 %d8 %u %f %f %s');
fclose(fid);
Specifiers:
%s : string
%n : double
%d : int
Note that textscan returns a cell array. Therefore C{1} is the cell array containing the surnames ‘Sally’, ‘Joe’ and ‘Bill’. C{1}(1) is the cell containing the string ‘Sally’. To convert this cell to a string (i.e. a character array), use the char function:
sallyString = char(C{1}(1));
The conversion is necessary e.g. if you want to write out the string using fprintf
Read file containing numbers and NaN
example:
nc = 6; % number of columns of data in the input file
fmt = repmat('%n', 1, nc);
clear m;
[m{1:nc}] = textread(filein, fmt);
m = cat(2,m{:});
Delimiters (textscan and textread)
Default delimiter:
' \b\t
‘
To use a different delimiter:
C = textscan(fid, '%s %n', 'delimiter', '\t');
If a nondefault delimiter is specified, repeated delimiter characters are treated as separate delimiters. When using the default delimiter, repeated white-space characters are treated as a single delimiter.
Skipping fields (textscan and textread)
Use asterisk: %*s instead of %s
Particularly handy when using textread where you can write the data directly into a list of variables
Error message
error('ERROR: wrong format');
Debug
Debug a function that requires arguments
Open the function file in the editor
edit function.m
Run the function from the command line
function(x,y)
For private functions:
edit private/privFunc.m
Commands
disp(val): display values while debugging (or simply val)
return : continue execution
dbstep : execute current line
dbquit : exit debugging
Measure time use
tic, toc: measure the time elapsed between invoking
Dates and times
Date formats
Date string: 24-Apr-2006
Serial date number: 732791
Date vector: 2006 04 24 0 0 0
Conversion functions
datenum: convert to a serial date. In Octave 1 Jan 0000 is defined as day 1.
datestr: convert to string representation of the date
datevec: convert to a date vector
Example: the 265th day of year 2007 is
datestr(datenum(2007,01,01) + 264)
Strings
Concatenate strings
newString = [string1, string2, string3];
Convert an integer to a string
newString = int2str(n);
Plot
Plot with dates
Example:
plot(dates, y);
datetick('x', 19);
Note1: dates must be a vector of serial dates (obtained with datenum)
Note2: 19 is one of a series of different formats that datetick can print the date into.
Note3: you might want to add ‘keepticks’ as a third input argument in order to get a good tick repartition.
Subplots
You can plot several plots in the same window with the subplot command.
Example – drawing two plots next to each other:
subplot(1,2,1);
plot(x1,y1);
subplot(1,2,2);
plot(x2,y2);
The two first figures indicate how the window is split, here 1 row and 2 columns while the third figure is the subwindow index.
Double axis graphs
In order to plot y1=f(x1) and y2=g(x2) with two Y-axes, copy the following lines:
line(x1,y1,'Color','r');
ax1 = gca;
set(ax1,'XColor','r','YColor','r')
ax2 = axes( 'Position',get(ax1,'Position'),. . .
'XAxisLocation','top',. . .
'YAxisLocation','right',. . .
'Color','none',. . .
'XColor','k','YColor','k');
line(x2,y2,'Color','k','Parent',ax2);
Plot several data sets
Each time you call the plot
command, it clears the plot window. If you want to keep the previous plot, you need to write
hold all
before the first plot
command and
Remember to write
hold off
after the last plot
command. That way, the plot window will behave “normally” on the next plot
call.
Grid
Examples:
set(gca, 'XGrid', 'on')
set(gca, 'XMinorGrid', 'off')
Write text in the plot window
The easiest way is to use the text
command:
text(xpos, ypos, str)
Check also out the annotations
command
Fix X and Y limits
xlim([xmin xmax])
ylim([ymin ymax])
Surface fitting
You have a non-uniform collection of measured points (from total station, differential gps, etc.) and you want to plot an interpolated surface.
Suppose your points are stored as X, Y, Z.
First you build a uniform grid based on the extremities of the measured area:
[Xm, Ym] = meshgrid (Xmin:s:Xmax, Ymin:s:Ymax);
where s is the grid size
Then you interpolate the surface at all the grid points:
Zm = griddata (X, Y, Z, Xm, Ym);
Eventually you may plot the surface:
surf(Zi)
BibTeX tips
Check also out LaTeX tips
Page number
To refer to a given page:
\cite[p. 25]{myref}
Multiple citations
\cite{myref1,myref2,myref3}
Citations in author-year style
Use natbib: include the following line at the beginning of the file.
\usepackage[authoryear,round]{natbib}
Author (Year) is obtained with \cite{myref}
while (Author, Year) is obtained with \citep{myref}
For the reference list, there are many possibilities. You might want to try out the following ones:
\bibliographystyle{abbrvnat}
\bibliographystyle{apalike}
\bibliographystyle{plainnat}
The advantage with abbrvnat
and plainnat
is that they display the “url” field. They also write the surname of the authors first and the year at the end of the reference. plainnat
writes out the full surname if available, which I find makes it harder to find references. In apalike
the reference starts with Lastname, S. (Year).
Authorless citations
Typically, technical reports from research institutes. A workaround is to write an acronym of the institution in the “author” field and then write the full name in the “publisher” field.
“Cited in”
I’ve chosen to simply write “Cited in…” in the “note” field. That way, “Cited in…” appears at the end of the reference.
LaTeX tips
Check also out BibTeX tips
Fonts
Bold
\textbf{this text written with bold fonts}
Italic
\textit{this text is italic}
Include graphics and images (Windows)
If you use Yap to visualise DVI outputs from LaTeX code, graphics and pictures should be in the EPS format. Many plotting programs, e.g. Matlab, are able to write EPS files.
JPG and PNG files must however be converted to EPS. ImageMagick works well for command line operations on pictures but I haven’t figured out how to obtain EPS files of reasonable size with it: the size of the EPS output is an order of magnitude greater than that of the JPG input.
For JPG to EPS conversion I’m therefore using a program called jpeg2ps with the following syntax:
jpeg2ps -h -r 600 file.jpg > file.eps
where 600 is the resolution in dpi. This produces an EPS file of approximately double the size of the JPG input.
For PNG to EPS conversion I’m using a script/batch called png2eps with the following syntax:
png2eps file.png 400 600 file.eps
where the first number is the height in pixels and the second number the resolution in dpi. png2eps requires the following libraries: libtiff, netpbm and zlib. They are available both for Linux and Windows.
Apparently it is possible to tell LaTeX to automatically convert JPG files to EPS using jpeg2ps, but I haven’t managed to get it to work. It also seems possible to include JPG and PNG files directly by using the PGF package but I haven’t managed to get it to work (“no BoundingBox” error).
Include graphics and images (Linux)
sam2p works both with jpg and png, provided you have the right libraries installed.
sam2p file.jpg file.eps
Unfortunately converted sketches are aliased. Another problem is that sam2p does not handle partial transparency, which is something I tend to use when I draw png sketches. Therefore I ended up using png2eps for converting png files to eps.
Output to PDF
You can convert dvi to pdf with dvipdfm:
dvipdfm file.dvi
Norwegian alphabet (æ, ø, å)
If you want to write your TeX-file with the Norwegian alphabet, include the following line
\usepackage[latin1]{inputenx}
As far as I understand, the latin1 encoding works for all European languages. Note that the tex file must be encoded the same way. In Emacs, for example, this means loading the Latin-1 language environment. Add the following line to the .emacs file:
(set-language-environment "Latin-1")
Finally, if you’re writing in another language, you probably want the hyphenation and section names (e.g. “Chapter”) to follow this language’s rules. Use the babel package:
\usepackage[norsk]{babel}
Rotate figures and captions
\usepackage{rotating}
Replace
\begin{figure}, \end{figure}
with
\begin{sidewaysfigure}, \end{sidewaysfigure}
Remember to replace ”width” with ”height” (or the other way around) in the options of the \includegraphics
tag.
In a twoside (\documentclass[twoside]{...}
) document, the rotation depends on the page side.
It is also possible to rotate tables by using
\begin{sidewaystable}, \end{sidewaystable}
Headings
Headings are included with
\pagestyle{headings}
This way, chapter, section, … titles are written at the pages top.
Some titles may be too long for the page width. One possibility is to include a short version in brackets when declaring the corresponding section:
\chapter[Short title]{This is a very long title}
The short title is also what will appear in the table of contents. The same syntax may be used with the \caption
tag to write a shorter caption for the list of figures / tables.
Add References to the table of contents
Unless you write the following, there will be no References / Bibliography entry in the table of contents:
\addcontentsline{toc}{chapter}{Bibliography}
You may want to write similar lines for other numberless parts (e.g. Nomenclature)
Margins
Use vmargin package.
\setmargrb{30mm}{30mm}{30mm}{35mm}
% left margin, top margin, right margin, bottom margin
For small margins:
\usepackage{fullpage}
List of figures / tables layout
The “report” template, which I’ve been using, sets the size of the space between the figure / table number in the corresponding list to 2.3em. That is narrow if you have figures numbered N.xx, let alone N.xxx. You may widen the space by adding the following lines somewhere before the \listoffigures command:
\makeatletter
\renewcommand*\l@figure{\@dottedtocline{1}{1.5em}{3.3em}}
\makeatother
The \makeatletter ... \makeatother
command is necessary because the renewed command—\l@figure
—contains an @.
In the example above the space is set to 3.3em.
Non-breaking hyphen and minus sign
From what I understand, non-breaking hyphens are written like this:
\mbox{-}
and non-breaking minus signs like this:
\mbox{--}
Inline equations
Inline equations are put between $-signs:
$x = 5$
is an inline equation
An inline equation requires more than one line of vertical space, e.g. with fractions will look bad unless you write:
\displaystyle
in the beginning of the equation.
Indentation of equations
By default, equations are horizontally centered. In order to align equations to the left of the page, instead, add the ‘fleqn’ option to the document class:
\documentclass[fleqn]{article}
If you want to indent equations from the left, you must also specify the value of the ‘mathindent’ variable:
\setlength{\mathindent}{1cm}
Vertical space
\vspace{2cm}
ffmpeg tips
NOTE: ffmpeg has been replaced by avconv, but commands below might apply to avconv as well.
Change the frame rate of videos
Example: convert a video file with any frame rate to a video file with 30 fps (frames per second)
$ ffmpeg -i inputfile.mp4 -r 30 -vcodec copy -acodec copy outputfile.mp4
Format videos for Vimeo upload
Use the following command
$ ffmpeg -i inputvideo.avi -vcodec libx264 -r 30 -b 5000k -s 1280x720 outputvideo.mp4
You will need the libx264 codec. For that purpose, install libavformat-extra-53 (Ubuntu 12.04).
You may have to convert the audio as well. If so, add the following:
$ -acodec libvo_aacenc -ab 192k
libvo_aacenc is installed with libavformat-extra-53
Split a video
$ ffmpeg -i inputvideo.avi -vcodec copy -acodec copy -ss 00:00:00 -t 00:30:00 output1.avi
Format videos for Windows movie player
Use the following command
$ ffmpeg -i infile -vcodec wmv2 -sameq -acodec wmav2 outfile.asf
Convert ogg to mp3
Make sure you have libavcodec-extra-53 installed. Then type
$ ffmpeg -i file.ogg file.mp3
Unfortunately, ffmpeg produces layer-2-mp3, which my mp3-player isn’t able to read.
imagemagick tips
Resize an image
convert image.jpg -resize 50% image_small.jpg
Convert jpg images to pdf file
convert *.jpg foo.pdf
Leica tips
LGO Software
Add UTM coordinate system
Tools > Coordinate System Management
Click on the Projection folder
Right pane: right-click > New…
Parameters:
- Name: UTM33
- Type: UTM
- Zone Number: 33
- Hemisphere: Northern
Click on the Coordinate Systems folder
Right pane: right-click > New…
Parameters:
- Name: WGS84-UTM33X
- Transformation: None
- Local Ellipsoid: WGS 1984
- Projection: UTM33
- Geoid Model: None
- CSCS Model: None
Predefine your own points
You’ve defined points on a map and you want to import them in the rover. It’s done in two steps
1. Import ASCII data to LGO
Write point data in an ASCII file. 4 columns: pointID, Easting or , Northing, Height
Open LGO
From the menu bar, choose Import > ASCII Data…
choose your data file and press the Import button
Step 1 of 4: usually you choose Free (default)
Next
Step 2 of 4: choose the separator
Next
Step 3 of 4:
Coordinate type: choose grid (for UTM), geodetic (for longitude – latitude) or cartesian (X, Y, Z).
Then don’t forget to label the column names by right-clicking and choosing PointId, etc.
One column must contain the Point Id.
Next
Finish
2. Write project to CF-card
From the LGO menu bar, choose Tools > Project Management
Right-click the project where you imported your points > Send To > Hard disk > For system 1200/GPS 900…
Choose the DBX folder of the GPS job you want to import the data to
Import projects
This functionality is useful e.g. if you move your data to another computer with a new installation of LGO.
Choose Tools > Project Management
In the right part of the LGO window, right-click and choose Register…
Locate the folder of the project and click OK
You might have to edit the coordinate system in the project properties in order to see your points
Modify antenna height of a measured point
In the project, choose the View/Edit tab (window bottom). Right-click the point and choose Edit intervals…
Modify the antenna height and click OK.
GIS/CAD Export
Under Windows Vista LGO has problems handling “pre-defined” folders locations e.g. the Desktop-folder. To avoid this problem, export to a different folder.
The following explains how to export LGO points with empty Code fields. Such points are exported as CAD blocks. You need to create a DXF template that defines the layout of the blocks. LGO is shipped with 2 templates but they don’t work too well therefore I recommend you use a modified version found here.
Open a project with the points you want to export. All points will be exported. If you want to export only some points, create a new project and copy the points into it.
Choose Export > GIS/CAD Data…
Right-click in the Lookup Table field and choose New. Name the lookup table. Choose whether you want to export the points’ Point Id and Elevation fields. If you do, you will be able to display these value in your CAD program. Click on the AutoCAD settings tab and browse to the DXF export template. Click OK.
Click on the Settings… button. The Coord type must be Local and Grid. In the AutoCAD tab, choose any AutoCAD version and the dxf format. Click OK
Click on the Lookup… button. In the Code Defaults tab, select the Export Points without codes box. Click OK.
Choose a filename and click Save.
Because of a bug in the GIS/CAD export module, you need to modify the exported DXF file by hand. Open it in a text editor and if you’ve been using my template DXF, replace all occurrences of:
“Point Id” with “POINT_ID”
“Elevation” with “ELEVATION”
(remove the double-quotes)
If you’re using AutoCAD, do a ATTSYNC after you open the file. Choose Name then Point. This should display the points with the pointer in Layer 0, the Point Id in layer POINTID and the elevation in layer ELEVATION.
If you want to create your own DXF export template, make sure that the Point Id attribute tag must be POINT_ID and the elevation attribute tag must be ELEVATION. Otherwise the tags are lost in the export and you lose control over the attributes.
Useful stuff
Avg. Limit Exceeded
In the Points tab, right-click on a column header and choose ‘Columns…’. Select ‘Avg. Limit Exceeded’. The points where the value is ‘Yes’ have been measured twice and differ with more than the average limit set for the job. By right-clicking on the point and choosing ‘Properties…’ then the Mean tab, you can select which point you want to keep.
Ambiguity Status (post-processing)
In the Results tab, check that the value in the ‘Ambiguity Status’ column of the measured points is ‘yes’. It means the point was calculated correctly.
Height reading
In the GPS-Proc tab, the value in the ‘Measurement type’ column tells how the antenna height is calculated from the input value. For some equipment there is an angle between the antenna reference point and the measurement point. In that case the value is ‘Slope’ and the true height is corrected accordingly. Otherwise the value is ‘vertical’.
Check if equipment is GLONASS ready
USER > STAT > System information then go all the way down the list and check that both GLONASS field values are ‘Yes’.
GPS 1200 equipment
Exit Smartworx
Press Shift then F6 to exit Smartworx and get access to the operating system (Windows something)
Linux tips
Tips for Ubuntu Linux. May work on other distros.
Basic shell commands (run from an xterm terminal)
man commandX
for the help manual of a command named commandX
ls
for the list of files in the folder
pwd
location of the folder you’re in
find
to look for a given file name
sudo
to run a command as an adminitrator
sudo apt-get app1
to install an application called app1
sudo dpkg -i package.deb
to install a deb package
df
to see disk usage
ufw
to manage a netfilter firewall
exFAT support
In order to be able to read from SDXC cards:
$ sudo apt-get install exfat-fuse exfat-utils
VPN
[tested with ubuntu 9.10]
install vpnc
$ sudo apt-get install vpnc
create a configuration file in /etc/vpnc/myconfig.conf, where myconfig may be any name you want. The file should look like this
IPSec gateway gateway.to.use
IPSec ID groupname
IPSec secret passwordforgroup
Xauth username myusername
Xauth password mypassword
Run vpnc
$ sudo vpnc myconfig
Stop vpnc
$ sudo vpnc-disconnect
Search for words in man pages
As in vi, press / to search for a keyword, then type the search pattern and Enter to search. Then n to search forward and N to search backwards.
Change display configuration from the command line
Find out what your displays are called by typing:
$ xrandr
In my case the laptop display is called LVDS and the external screen is called VGA-0
To turn off the laptop display and turn on the external screen, I simply write:
$ xrandr --output LVDS --off --output VGA-0 --auto
Python tips
Check also out the excellent documentation at python.org
Environment
Reimport a module after modifications:
import imp
imp.reload(mymodule)
Append directories to your Python Path:
import sys
sys.path.append("home/mydir") # UNIX path
sys.path.append("D:\\data\\mydir") # Windows path
In order to run self-coded functions, you need to place them in a module and then import the module. A module is a folder containing a file called __init__.py
If you want to be able to call
from module import *
the file must contain a definition of the
__all__
variable:
__all__ = ["func1" "func2"]
where the brackets contain the list of submodules (remember to keep it up-to-date). In the example above, the submodule files are ‘func1.py’ and ‘func2.py’.
The file may also contain code that you’d like executed each time the module is imported.
Importing a module:
import module
You may still import submodules if __init__.py is empty:
import module.submodule
Useful commands
>>> import os
>>> os.getcwd() # returns current folder path
>>> os.chdir('newdir')
>>> execfile('myfile.py')
IDLE shortcuts
Alt-P: previous command
Alt-N: next command
Syntax
# this is a comment
List
>>> a = ['word', 100, 13]
>>> a[2]
13
>>> len(a)
3
>>> range(5)
[0, 1, 2, 3, 4]
Functions
def fib(n):
"""Print a Fibonacci series up to n."""
a, b = 0, 1
while b < n:
print b,
a, b = b, a + b
Coastal-sea-ice action on a breakwater in a microtidal inlet in Svalbard (PhD)
PhD
Defence
- Trial lecture
- Defence video
- Defence slideshow
- Defence slideshow with notes
- Questions from the opponents
Articles
- 19th IAHR International Symposium on Ice (2008)
- Ninth International Conference on Permafrost (2008)
- Kristensen, L., Christiansen, H., Caline, F., Temperatures in Coastal Permafrost in the Svea Area, Svalbard (PDF)
Pictures
- 2005
- 2006
- 2007
- 2008
- 2009
- equipment
- erosion
- geosynthetics
- gravel islands
- ice
- presentations
- stress sensors
- thin sections
- Svea
Other
- The end of the tunnel — a look back at my PhD
- Data
- Matlab functions for converting different data types
- Caline, 2000, Modelling permafrost temperature response to variations in meteorological data (MSc)
- Abaqus tips
External links and articles
- Lehr, 1985, Let there be stoning — an inspiring article about scientific presentations
- Measurements and uncertainties (av8n.com)
- The Science of Scientific Writing, Gopen and Swan, 1990
- Time and date standard
- CiteULike.org – a website for organising and sharing your reference lists
- Dr. Bookspan: how to fix your own neck pain
Contact: Fabrice Caline — +47 47293868 — fabrice.caline@gmail.com