Python is a very-high-level dynamic object-oriented programming language. It is designed to be easy to program and easy to read. It was started in 1980, and has since gained popularity in a broad range of fields from web development, games, usage as a scripting language, and of course science and engineering.

With the sole exception of Simulink, Python can replace Matlab. And while Matlab licences are VERY expensive, Python is completely free - as in free beer! A pretty fair comparison of the two can be found under https://sites.google.com/site/pythonforscientists/python-vs-matlab.

A short introduction to Python is given in the Appendix.

Developed by National Instruments, LabVIEW is often used for data acquisition and for real-time control systems. After C, it is the most frequently used application for data acquisition. (http://www.ni.com/labview/)

MATLAB is a great environment for solving many practical problems you will encounter in your work-life. Since it is also very widely used in practice, I will use it consistently for the exercises accompanying this lecture. Nevertheless, don’t forget that Matlab also has disadvantages, and that there are also other good and helpful tools for data analysis. The main disadvantages of Matlab are, in my opinion: i) its high price, and ii) its somewhat outdated programming structure, which is inherently functionally oriented and not object oriented.

The following locations are very helpful for getting started with Matlab:

- The complete documentation of all toolboxes, including the often highly recommendable printed documentation (PDF format), can be found at http://www.mathworks.com/access/helpdesk/help/helpdesk.shtml.
- If you have not worked with Matlab before, the Matlab Introduction is a good point to start (http://www.mathworks.com/access/helpdesk/help/techdoc/matlab.shtml)
- At the home-site of Matlab you can also find under MatlabCentral free programs from other Matlab users, which address many problems you will encounter with your work.
- A very recommendable guide to good programming style is “Matlab Programming Style Guidelines” (by Richard Johnson). While it focuses on Matlab programming, most points are also valuable for programming in other languages.

In addition on your Moodle site for this course you find “matlab_tutorial.pdf” is a short introduction to Matlab (comparable to the “Matlab Introduction” mentioned above).

These are REALLY IMPORTANT !!!

Command | Documentation |
---|---|

ver |
Tells you which version and what toolboxes are installed. |

help |
Like helpwin, but does not open a new window. |

helpwin [name] |
If name is empty, this lists the different help-topics. If name is the name of a toolbox, it lists the commands in that toolbox. If name is a command, it lists the first help-block for that command. |

doc [name] |
If name is empty, it starts the help-browser. If name is a command, it lists the HTML-help for that command. |

which [name] |
Tells you if name is a variable or a file (and its location) |

[name]-TAB |
Tells you which commands start with name (very helpful!) |

I typically start out with *name-TAB*, to check if I might be able to
guess the command. If I find a likely command, I use *doc* to find out
the details; otherwise, I use *helpwin* to narrow down my search. Then I
copy an example from the documentation to my command window, and modify
it there as I need it.

In addition, the integrated Help-browser (opened by clicking on \(fx>>\) at the beginning of the command line) offers help and information access.

In addition, to improve your code you can

- Check the warnings and errors that are indicated on the right hand border of the Matlab Editor.
- Use the M-Lint code checker to detect syntactic errors or conspicuous code segments.

Also, remember that you should not re-invent the wheel when you don’t have to. There is lots and lots of Matlab code freely available. A very good place to start is http://www.mathworks.com/matlabcentral/.

In general, you should select one editor that you use by default for your writing and editing tasks, and make sure that you know this one editor well. Personally, I have decided to use vim, a very powerful, keyboard-oriented editor. If you prefer slower but simpler mouse-based input, check out the free http://notepad-plus-plus.org/.

Either way, make sure that you know your editor well!

You are going to be using the Matlab editor a lot, so make sure that you are very comfortable with the most important keyboard shortcuts:

- F5
- Run the current program.
- F9
- Execute currently marked code.
- F12
- Inserts a break-point at the current location.
- CTRL+A CTRL+I
- Properly indents all your loops. This really helps to find mistakes in ending if, for, or while blocks.
- CTRL+R
- Comment out the marked code.
- CTRL+T
- Remove comment sign for marked code.

Also, make sure you learn how to use the debugger. It will help you massively to get your programs running more quickly.

The *Matlab Workspace *contains all the variables the Matlab currently
knows, and can be checked with either the command *who* or *whos*. For
example, if you type

```
clear;
time = 0:0.1:10;
x = sin(time);
```

then your current workspace contains only the elements *time* and *x*:

```
Name Size Bytes Class Attributes
time 1x101 808 double
x 1x101 808 double
```

**Scripts** When you start Matlab up, you are in the *Base Workspace*.
There you can execute commands either by typing them in, or by putting
them into a file and typing the filename. For example, if you put the
three lines

```
clear;
time = 0:0.1:10;
x = sin(time);
```

into a file called *myScript.m* , you can achieve the same result as
above by simply typing *myScript* on the command line. Such a file is
called a *Matlab script*, and all the commands in the script are
executed just as if you would type them in, and the base workspace has
the same elements as in the example above.

**Functions** Now let’s add an additional line starting with the word
*function*, and save the file with the name *myFunction.m* . This file
now contains the lines

```
function myFunction()
clear;
time = 0:0.1:10;
x = sin(time);
```

If you now type *myFunction* you will find that - your base workspace is
empty! The reason is that Matlab executes each function in its own, new
workspace. All the variables that you need you have to provide as Input,
and all the variables that you want to keep from this function you have
to keep as Output. Note that all other variables are deleted when the
function closes. For example,

```
function myOutput = myFunction(myInput)
% Display a comment
infoText = 'Just calculating the sine for you.';
disp(infoText);
% Calculate the sine of the input
myOutput = sin(myInput);
```

would take the values “myInput”, and return the values “myOutput”. (Lines starting with “%” are comments, and have no effect on the calculations.)

For example, you can call this function as follows:

```
time = 0:0.1:10;
x = myFunction(time);
```

Important: NEVER leave a space before the brackets of a function call. This often - but not always - leads to wrong results.

*Workspaces in Matlab: Note that the Base Workspace never sees any variable with the name infoText or myInput. Similarly, the function myFunction never knows any variable time, but only the variable myInput.*

**Vectors and Matrices** Matlab is strongly matrix oriented, which often
allows you to avoid for-loops. Use this notation, wherever you can!

Example:

```
>> A = randi(10,3)
A =
7 1 1
2 3 9
8 1 7
>> b = randi(10,3,1)
b =
4
10
1
>> A*b
ans =
39
47
49
```

**Structures** are simply variables with named sub-fields. They are
often used to group information that belongs together. For example, if
the three variables

```
DataValues = sin(1:10);
DataInfo = 'This is a sine-wave';
DataSourceID = 247;
```

belong together, you could group them into a structure called *data*:

```
data.Values = sin(1:10);
data.Info = 'This is a sine-wave';
data.sourceID = 247;
```

Then the Matlab structure *data* contains everything that you want; and
you can easily access the individual elements, for example the command
\(absValue = abs(data.Values);\) would return the absolute value of
the data-values.

**Cells** are very similar to Matlab arrays. The difference is that
while each element of an array has the same format, different elements
of a Matlab cell can have different formats.

To address elements of arrays, you use “( ... )”, but for cells you use “{ ... }” For example:

```
myArray = 1:3;
```

contains 3 double values; and you address e.g. the second value by

```
myArray(2)
```

In contrast,

```
myCell = {'abc', 1:3, rand(2)}
```

contains a string, a vector, and a marix; and you address the second element (i.e. the vector) by

```
myCell{2}
```

Cells are also commonly used when you have to deal with strings of different length:

```
>> firstNames = {'Sam', 'Peter', 'Alexander'}
>> firstNames(3)
ans =
'Alexander'
```

Before you start programming, spell out the steps you have to do, and write them down as comments. (E.g.

*Set the parameters – Select the input file – Read in the data – Filter the data — Show the results — Save the results to an outfile — Show the user the location of the outfile*). Not only does this help you to organize your code, it also provides a first rudimentary documentation of your program.Data analysis is an interactive task. Make use of the very powerful Matlab environment for interactive programming, and first develop your analysis step by step on the command-line.

Once you have your data analysis - for the one block - going, grab the Matlab history, and turn it into a function. Think about what you want/need for the input, and what the output should be.

Before you implement a mathematical algorithm, write it down on paper! This makes the implementation much quicker, because you have to spell out what you want to do.

Use the Matlab Help – it is one of the best parts of Matlab. Especially, use the code samples from the Help and from the Demo examples.

If possible, use some simple dummy data to test your programming steps.

Be careful with your choice of variable names. For example, never use “i” as a variable – use “i” instead (also “jj” instead of “j”, etc.)

for ii = 1:10 disp(ii); end

In the Matlab help browser it is often very helpful to narrow down the search-range to a few toolboxes. This can be done in \(File | Preferences | Help | Enable Product Filter\).

When you write your own functions, stick to the Matlab-style of documentation: The first line starts with the name of the function/script, and contains a one-line description. The next section gives more detailed information.

Never use generic words, such as \(integer\), \(line\), etc as variable name: you might hide existing Matlab functions to your program! As a specific example, test the effect of a variable named \(line\): if you generate a variable \(line\), then you don’t see the function any more!

which line >> built-in (C:\Program Files\MATLAB\R2012a\toolbox\matlab\graphics\line) line = 'This is a line.'; which line >> line is a variable.

Know your editor well – you are going to work with it a lot! Especially, know the keyboard shortcuts!

Make sure you know the basic Matlab syntax. Try to use matrix multiplications instead of loops wherever possible: this makes your code nicer, and your programs much faster.

Once you know the Matlab basics, get to know the

*Debugger*. It really helps to find mistakes in your programs.Don’t repeat code. If you have to use a piece of code more than two times, write a Function instead.

You should ALWAYS document the code that you write – even if you only hack a small program! Thereby your file-header should follow certain rules (the parts with “[]” are to be filled in):

```
[First a 1-line description of the program]
[A more thorough description]
[An example how to call the program]
[A description of the inputs and outputs]
Author: [xxx]
Version: [xxx]
Date: [xxx]
```

**Example**

```
%addMe Calculates the sum of two numbers
% This is a short demo program. It only works for numbers, vectors,
% and matrices.
%
% Call: summed = addMe(a,b)
% E.g.: out = addMe(1,2)
%
% Inputs:
% a ... scalar, vector, or matrix
% b ... scalar, vector, or matrix; has to match "a" in size!
%
% Output:
% added ... Sum of the inputs
%
% Author: Thomas Haslwanter
% Version: 1.0
% Date: 12-Sept-2012
%*****************************************************************
function added = addMe(a,b)
added = a + b;
```