`MATLAB`

syntax¶This script will introduce you to the syntax and various operations in the
MATLAB analysis language as well as introduce you to some image processing.
and sizing up *E. coli*. There are many different programming languages
(`python`

, `C++`

, `java`

, `julia`

, `matlab`

, etc.) that are all very useful for
scientists in every discipline. While we will use Matlab for this course,
We urge you to not subscribe to any language with religous fervor.

There are several options, buttons, and windows in the Matlab GUI. The two
most useful windows for you will the the editor (scripting window) and the
command window (`>>`

). The editor is where we will spend most of our time as
we write various scripts to perform analysis, simulations, and
calculations. However, the command window is good for testing small
snippets of code. The command window does not store your entries which is
why the editor window is so useful. To learn some of the Matlab syntax,
let's enter a few things into the command window.

In [1]:

```
1 + 1 % Should give us 2
```

In [2]:

```
2 * 8 % I believe this should be 16
```

In [3]:

```
exp(2) % This should be around 7.4
```

In [4]:

```
a = 10
```

Once a variable is defined we can do operations with it.

In [5]:

```
a * 4
```

And again save the output of these operations as a new variable.

In [6]:

```
b = a * 4
```

In [8]:

```
b + a * 5
```

`;`

) to the end of the line.

In [9]:

```
c = a^100 % This will be printed
d = a^200; % This will NOT be printed
```

`vector`

or `array`

variable.

In [11]:

```
values = [0, 1, 1, 2, 3, 5, 8]; % Fibonacci sequence!
```

`()`

.

In [12]:

```
values(3) % This should be a 1 since the third element of the array is 1
```

It is interesting to note that matlab stores everything as matrices by default. As a matter of fact the name MATLAB comes from *MAT*rix *LAB*oratory, not mathematics laboratory as people generally think. Our variable `values`

is actually a 1D matrix for example. We will exploit this feature a lot throughout the course.

There are several ways to generate arrays. A very useful one is to generate evenly spaced intervals. The syntax in matlab to generate this type of array is

`begin:step_size:end`

Let's look at a couple of examples

In [15]:

```
intervalOne = 1:1:10 % This will print numbers from 1 to 10 with spacing of 1
```

In [16]:

```
intervalTwo = 1:3:10 % This will print numbers from 1 to 10 with spacing of 3
```

Now that we have the basic syntax of Matlab down let's give it a test run on some real data.

In the following lines of code, we will use an image of a graticule to measure the distances between pixels of a camera. We will then use this value to place a scale bar on an image of cells, **as should always be done in any scientific image**.

We will begin by reading in the image of the graticule. Remember, an image is just data -- a simple two-dimensional array in which element corresponds to a pixel value.

In [25]:

```
% Change the working directory to where our data lives in case we are not there
cd('~/Documents/PhD/RPGroup-PBoC_github/mbl_pboc_2016/data/sizing_up_ecoli/')
% Read the image of the gradicule
gratIm = imread('Graticule100x.tif');
```

`imshow`

. Remember that an image is just a matrix with numbers. What `imshow`

does is simply assign a color to each of these numbers on a gray scale

In [26]:

```
imshow(gratIm);
```

In [27]:

```
imshow(gratIm, []) % This should be a rescaled image.
```