## A Brief Introduction to MATLAB

MATLAB is a large numerical and symbolic analysis package installed on the EWS system. You can access Matlab from the GUI of Linux by clicking "Applications" drop down menu, then "EWS software" followed by "Matlab".

### Image Processing Functions

To obtain a list of image processing commands available under MATLAB, type   help images   at the MATLAB prompt. Images are included with the image processing toolbox (IPT). Some image's names: "clown," "trees," and "forest."

To load an image, type   load image_name   where image_name is the name of an image. This command creates the two variables X and map. X is an array of values that indexes a color map contained in map.

To display the images type   imshow(X,map)   to display image X with colormap map. To load 8-bit grayscale images not included in the toolkit (e.g. tank_01.raw, fn002.raw, etc.) type the following:
fid = fopen('image_name','r');
image = image' / 255;
The first command assigns a file pointer to the image file "image_name." The second command reads the image from a file into an array image of size xsz by ysz. The last command takes the transpose of image and scales it for display purposes.

Note: you may need to type   truesize   to display the image at its normal size (MATLAB often ignores an image's dimensions and uses some default size instead).

Finally, type   imshow(image,256)   to display the image. The argument "256" tells Matlab to display the image with a grayscale map of 256 different gray scales.

### How to Display a Video Sequence In Matlab?

Suppose you have the frames X_i where i = 1,2,..,N . Each of these frames are images themselves and should be of the same size. The assumtion is that all pixels within these frames are between 0 and 1 (normalized) and transposed so that imshow(X_i) works for each frame. Then there are 2 ways to display these frames as a video sequence

#### 1st Way

for i=1:1:N,
Do processing to form the frame X_i
X(:,:,1,i) = X_i;
end;
mov = immovie(X*256,gray(256));
movie(mov);

#### 2nd Way

for i=1:1:N,
Do processing to form the frame X_i
image(X_i);
mov(:,i) = getframe;
end;
movie(mov);

### MATLAB Functions and Macros

MATLAB is a high level language and lets you write functions to make programming easier. A MATLAB file must have the file extentsion ".m" (e.g., my_program.m). Let us call such a file an "m-file". To use a MATLAB function that has been saved as an m-file, type the function name at the MATLAB prompt without the extension. For example, suppose we create an ASCII text file "fun.m" that contains the following function:
function[test1, test2] = fun(a, b, c)
statement;
statement;
end
To use "fun", type:
[var1, var2] = fun(a1, a2, a3);
where a1, a2, a3 are passed to the function "fun", and var1, var2 are values returned by the function.

An m-file does not need to contain a function. It could just contain a series of MATLAB commands that is executed when it is loaded.

MATLAB takes a _very_ long time to execute "for" loops. Try to avoid "for" loops by using MATLAB's built-in matrix functions; MATLAB is optimized for array processing. Take advantage of these functions to reduce your software development time.