MATLAB Lesson 0 - Using MATLAB


You will need to be familiar with creating, editing and executing M-files. This allows you to

M-files are just plain text files containing MATLAB commands. An M-file has extension .m for example cubic.m.

An M-file is executed, that is all the commands within the M-file are executed, simply by typing the name of the file (without the .m extension) in the command window.

Downloading M-files

M-files will be provided as examples during your course. You will need to be able to download these from your course web page.

To do this use one of the following methods:

Save the M-files in an appropriate folder (directory), for example one for each of the courses you are doing.

Download the MATLAB file cubic.m (right click the link and use Save Link As ... or Save Target As ...) and save it in MATLAB's current working folder.

This lists the M-files in the current working directory. Also check the Current folder panel.

This gives the present (current) working directory (folder).

>>  what
>>  pwd


MATLAB comes with an editor that allows you to easily create and edit M-files. The basic functionality is just like most editors (word processors). However the MATLAB editor also has features useful specifically for writing programs:

Creating a new file

The are several ways of creating a new M-file using the MATLAB editor. These include:

Editing an existing M-file

The are several ways of opening an existing M-file for editing with the MATLAB editor, including:

Open the file cubic.m in the MATLAB editor. (See above if you have not already downloaded this file.)

This opens the file cubic.m for editing.

Alternatives are to use the Open menu item or to double click on the file name in the Current Folder sub-window.

>>  edit cubic.m

Opening the M-file cubic.m in the MATLAB editor produces a new window similar to the one below:


Do not worry about the meaning of the MATLAB commands for now, but you should try executing the M-file by typing cubic in the MATLAB Command Window.

Matlab demos

MATLAB has a variety of examples and demos, including videos, which explain some of its features.

Find the introductory MATLAB demos.

This opens the Help Browser with a list of basic MATLAB video demos.

>>  demo matlab

At this stage the "Getting started with MATLAB" video may be particularly useful.

Self-test Exercise

Download the M-file parabola.m and open it in the MATLAB editor, then execute the commands in the file.

After downloading the file type
edit parabola.m
in the MATLAB command window.
Use the mouse to select the text between the word "Answer" and here to see the answer.


M-files are text files with an extension .m containing MATLAB commands.

The MATLAB editor is a good way to create and edit M-files.