## M-files

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

- Store commands for later use. Will you remember in Week 10 the commands you used in Week 2?
- Build up larger and more complicated programs.
- Use the MATLAB editor to debug and then optimize your program.

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:

- Left click on the name of the M-file and chose the option to save the file in your account or on your computer.
- If the .m extension is not recognised (for example if MATLAB is not installed on your home computer), then Right-click on the name of the file and choose the "Save Target As ..." (IE) or "Save Link As ..." (Firefox) option.

`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 **p**resent (current) **w**orking **d**irectory (folder).

## MATLAB Editor

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:

**Syntax highlighting****Comments**(anything beginning with a %) is coloured green. It it very important to use comments to document your M-files, just as MATLAB does.- MATLAB
**keywords**are coloured blue. You should not try to change a keyword. **Smart indenting**can be used so the structure of your programs becomes clearer.

**Debugging****Breakpoints**can be set on any line of an M-file so you can check the values of your variables and more easily find any mistakes.-
**Profiling**can be used to find where your program is spending most of its time, allowing you to produce more efficient code.

### Creating a new file

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

- Click the
**New Script**icon (a page with a + sign) at the top left of the Home tab. - Use the
**New**(a large yellow plus + sign) icon immediately to the right of the**New Script**icon. This enables you to create a variety of new objects, in particular**scripts**and**functions**(see Lesson 4). - In the MATLAB command window type edit filename.m.

### Editing an existing M-file

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

- Click on the
**Open**icon, the yellow open folder close to the top left of the HOME tab in the MATLAB window or the EDITOR tab of the MATLAB editor. Then select the appropriate file. - In the MATLAB command window type edit filename.m. If the file filename.m already exists it will be opened, otherwise you will get a dialogue box asking if you want to create it.
- If you have Current Folder window open, then double clicking a file will open it in the MATLAB editor.

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.

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.

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

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.

Answer:

After downloading the file type

edit parabola.m

parabola

in the MATLAB command window.

Use the mouse to select the text between the word "Answer" and here to see the answer.

## Summary

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.