MATLAB Resources

Accessing MATLAB

If you have an account on, you can access MATLAB in one of two ways. One is by logging on to a computer in the PRISM lab (RH 421), opening a terminal and typing matlab at the prompt.

It is also possible to access MATLAB remotely. One way to do this in Windows is to install a unix emulator Cygwin/X. Then open a terminal and type at the prompt
 > DISPLAY=yourcomputername:0 export DISPLAY 
Then ssh to the prism server by typing
 > ssh -X 
Finally, to run MATLAB (hopefully with graphics!), type
 > matlab & 
(Thanks to Izzy for pointing out this solution)

Note: on a Mac, it shouldn't be necessary to install a unix emulator.

A Faster Way of Remotely Accessing MATLAB

Again from a terminal, ssh to the prism server by typing
 > ssh -Y 
To run MATLAB, type
 > matlab -nojvm 
(Thanks to Arvind for this trick)

Note: This should be faster than the previous method because it runs MATLAB in the terminal, while still retaining the ability to display plots as separate figures.


  • The Help Command
    Type help at the MATLAB prompt for a list of help topics.
     >> help 
    For help with a specific function, type help followed by the name of the function. For example, for help with the imread function, type
     >> help imread 
  • The Help Menu
    Product help under the help menu in MATLAB contains complete documentation, examples and demos for all the built in functions. This includes the toolboxes, with the image and signal processing toolboxes being of particular interest.

Online Resources

Quick Start: Working with Images

  • Reading an Image
    The function imread can be used to read an image from a file and store it as a matrix. For example,
     >> f = imread('myimage.bmp'); 
    stores myimage.bmp as a matrix f. Other common image formats can be handled as well. The size of f can be found by typing
     >> size(f) 
    It may be an M by N matrix in the case of a grayscale image, or for color images it may be an M by N by 3 matrix containing the RGB components at each pixel. We can use the rgb2gray function to convert an RGB image to a grayscale image by typing
     >> fgray = rgb2gray(f); 
    For most computations we would like to work with data of type double, but images by default are read as type uint8, integers ranging from 0 to 255. To convert type uint8 to type double we can use the built in double function, which converts values to double precision.
     >> h = double(fgray); 
  • Existential Questions
    View information about the current variables, including their types, by typing
     >> whos 
    Answer the question: "why?"
     >> why 
  • Viewing Images
    The imagesc and image functions can be used to display data as an image. Both map the data to colors defined by a colormap. A colormap is a matrix with three columns, with each row representing the RGB components of a color. A colormap for grayscale images with intensities ranging from 0 to 255 can be defined by
     >> colormap(gray(256)); 
    To display the matrix h as an image, imagesc scales the data to use the full range of the defined colormap:
     >> imagesc(h) 
    On the other hand, the image function
     >> image(h) 
    uses the elements of h as indices into the colormap. When using the above gray colormap, this should display the true appearance of grayscale images represented as matrices with intensities ranging from 0 to 255.
  • Writing Images to a File
    The function imwrite can be used to write images to a file. See the help for details on how to write images of different formats. To write the data h as a grayscale .bmp image for example, imwrite can be used with three arguments: the data (preferably in uint8 format), the colormap and the name of the image.
     >> imwrite(uint8(h),gray,'myimage.bmp'); 
  • Saving and Loading Workspace Variables
    The save function can be used to save some or all of the variables in the current workspace as a .mat file. The entire workspace can be saved as stuff.mat by typing
     >> save stuff 
    Or just a couple variables, say f and h, could be saved as f_and_h.mat by typing
     >> save f_and_h f h 
    These .mat files can be loaded at a later time by typing
     >> load stuff; load f_and_h 
  • Scripts and Functions
    The MATLAB editor can be opened by going to File -> New -> Script/Function or by typing edit at the prompt. Some existing functions can be edited as well, for example
     >> edit why 
    A script is just a list of commands that could otherwise have been typed at the prompt. By typing them into the editor, they can be saved as a .m file, myscript.m for example. Then typing
     >> myscript 
    is equivalent to typing in all the commands listed in myscript.m. A function on the other hand has specified input and output arguments. It has the form
    function [ output_args ] = myfunction( input_args )
    Saved as myfunction.m, it can be run from the prompt by typing
     >> [output_args] = myfunction(input_args)