Sizing up E. coli

Credit: Griffin Chure

Basic 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
ans =

     2
In [2]:
2 * 8 % I believe this should be 16
ans =

    16
In [3]:
exp(2) % This should be around 7.4
ans =

    7.3891

This are simple examples of basic operations one can do with Matlab. But at the same time, we can store values in memory as variables. Let's look at a simple example.

In [4]:
a = 10
a =

    10

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

In [5]:
a * 4
ans =

    40

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

In [6]:
b = a * 4
b =

    40

I'm sure you got the idea, but just for completeness I would add that you can do operations using only variables; for example

In [8]:
b + a * 5
ans =

    90

Notice that every entry we have made so far has automatically been printed. We don't always want to see this, especially when we are generating huge tables or loading images. To suppress output, we simply need to add a semicolon (;) to the end of the line.

In [9]:
c = a^100 % This will be printed
d = a^200; % This will NOT be printed
c =

  1.0000e+100

Another very useful feature from matlab is the ability of storing several entries into a single vector or array variable.

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

But what if I wanted to access let's say the 3rd element of this array? Well we can index the values on the array using parenthesis ().

In [12]:
values(3) % This should be a 1 since the third element of the array is 1
ans =

     1

It is interesting to note that matlab stores everything as matrices by default. As a matter of fact the name MATLAB comes from MATrix LABoratory, 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
intervalOne =

     1     2     3     4     5     6     7     8     9    10
In [16]:
intervalTwo = 1:3:10 % This will print numbers from 1 to 10 with spacing of 3
intervalTwo =

     1     4     7    10

Sizing up E. coli with real data.

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');

Let's take a look at the image to see what we are dealing with. For this we will use the function 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);

Oh no! Our image appears to be only black! This is because the image is actually 12 bit, but is being displayed as a 16 bit image. There is information in those pixels, but they are simply being displayed on a different scale. We can rescale all of the pixels in this image as follows.

In [27]:
imshow(gratIm, []) % This should be a rescaled image.