CS 332

Lab 1

The purpose of this lab is to learn how to work in the MATLAB environment, and how to read and display images in MATLAB.

Start MATLAB

On your MAC, click on the MATLAB icon that appears in the Applications folder on the dock at the bottom of the screen.

The default MATLAB desktop contains four inner windows:

Like other windows-based applications, you can change the size of the inner windows by dragging their borders, close the inner windows, and use scroll bars to navigate up and down the window contents. If your MATLAB windows change their appearance in an undesirable way, you can restore the original setup by selecting Desktop Layout>Default from the Desktop menu at the top of the main window.

Enter some commands in the Command Window

MATLAB is interactive - the user enters commands in the Command Window, MATLAB executes these commands, and the results are returned and printed. Enter some commands in the Command Window (the % symbol precedes comments that you do not need to enter):


num = 10              % create a variable "num" with the value 10

values = [1 2 3 4 5]     % create a 1D array (vector) of 5 numbers

num                   % print the value of an existing variable

format compact        % omit vertical spaces in future printout

a = 2

clc                   % clear the Command Window

b = 20.0;             % a semi-colon at the end of a statement
                          % suppresses printout of the value

whos                  % list the variables currently defined
                          % in the MATLAB workspace

c = (2 * 3) + num         % perform simple calculations

num = sqrt(c^3 + b^2)

val2 = [3 8 -4; -1 0 2]   % create a 2x3 matrix of numbers
              % note the semi-colon in the middle!

size(val2)            % get the dimensions of the matrix

You can also observe the variables listed in the Workspace window.

Use the Command History to re-execute earlier commands

Execute a previous command again by double-clicking on the command in the Command History window, or by copying-and-pasting the command into the Command Window and hitting the carriage return.

Use the arrow keys for command execution and editing

Use the four arrow keys to do the following:

Once a command is complete, it can be executed by hitting the carriage return, regardless of the position of the cursor in the line.

Open the MATLAB editor

MATLAB has a built-in editor for creating and viewing code files and text files. There are at least three ways to open an initial editor window:

Create a script file of MATLAB code

MATLAB code files are referred to as M-Files and have a .m file extension. In this lab, we'll create a type of M-file called a script, which contains a sequence of MATLAB commands that can be executed all at once. Executing a script produces the same results that would be obtained if the individual commands were entered, one-by-one, in the Command Window.

First create a folder named lab1 on your Desktop, or in the TEMPORARY SAVE FOLDER on the Desktop, where you will save the code file that you create. In MATLAB, make this folder your Current Folder or Current Directory. You can do this either by using the arrow icons at the top of the Current Folder (Directory) window to navigate up and down the directory tree, or by using the pull-down menu along the top menu bar of the main MATLAB window, together with the folder-and-up-arrow icon to the right of this pull-down menu. The Current Folder (Directory) window should show the empty contents of your lab1 folder.

In the editor, type some assignment statements, similar to those entered earlier in the Command Window. Save the script in one of the following two ways:

A dialog box will appear where you can enter the name of the script file - enter the full file name (e.g. lab1.m) in the text box labeled with Save As:. The box labeled Where: should contain your lab1 folder. Click on the Save button, and your file will be stored in your lab1 folder - check its listing in the Current Folder (Directory) window. Now execute the script in the Command Window by entering the first file name lab1, and note the printout of the variable names and values. Enter whos to see that your script variables now exist in the MATLAB workspace.

Edit the script (for example, add semi-colons at the end of each statement), save the modified file, and execute it again in the Command Window. As a shortcut, you can save the file by clicking on the floppy disk icon at the top of the editor window. You can also run your script by clicking on the green arrow icon at the top of the editor window.

An existing M-File can be opened in the MATLAB editor by (1) selecting Open... from the File menu and navigating to the desired code file, (2) double-clicking on the file name in the Current Directory window, or (3) invoking the edit command in the Command Window:

   edit lab1

The file (lab1.m in this case) must be stored in the Current Directory, or exist somewhere on MATLAB's search path.

Load and display an image

Use the imread function to load an image into MATLAB, and give it a name:

   coins = imread('coins.png');      % note the semi-colon!

(The coins.png image comes with MATLAB's Image Processing Toolbox.) The image is now stored in a 2D matrix named coins - you can check its size by entering the whos command or checking the list of variables in the Workspace window. There are two ways to display an image. Use imshow for quick viewing:

   imshow(coins)

The image will appear in a separate figure window. Closer examination of the image is possible with imtool:

   imtool(coins)

Two windows will appear, an Image Tool window and a smaller Overview window (if the Overview window does not appear automatically, select Overview from the Tools menu). Try the following:

Examine the changes in image intensity between adjacent image locations, around the borders of the coins and along the edges of the raised figures on the surface of the coins. Also note that even in regions that appear to have constant intensity, such as the dark background, the intensities do not have a constant value. Our first step in analyzing images will be to detect and describe the changes in image intensity - this information provides the first hint about the structure of the scene being viewed.

Close all of the open windows by executing the following two commands in the Command Window:

   close all
   imtool close all

Quit MATLAB

When you are done, quit out of MATLAB by selecting Quit MATLAB from the File menu.