Skip to content Skip to navigation

Connexions

You are here: Home » Content » A Very Brief Introduction to Programming in MATLAB

Navigation

Recently Viewed

This feature requires Javascript to be enabled.

A Very Brief Introduction to Programming in MATLAB

Module by: Darryl Morrell. E-mail the author

User rating (How does the rating system work?)
Ratings

Ratings allow you to judge the quality of modules. If other users have ranked the module then its average rating is displayed below. Ratings are calculated on a scale from one star (Poor) to five stars (Excellent).

How to rate a module

Hover over the star that corresponds to the rating you wish to assign. Click on the star to add your rating. Your rating should be based on the quality of the content. You must have an account and be logged in to rate content.

:
(0 ratings)

Summary: This module gives a very brief introduction to some of the concepts behind programming in MATLAB using m-files.

Note: You are viewing an old version of this document. The latest version is available here.

You can use MATLAB scripts to automate computations. Almost anything typed at the command line can also be included in a MATLAB program. Lines in a MATLAB script are interpreted sequentially and the instructions are executed in turn. This simplifies repetitive computations. This allows you to create complex computations that cannot be readily implemented using commands at the command line. You can also create computational capabilities for other people to use.

MATLAB scripts are text files and can be edited by any text editor. Any script should run the same on any computer running MATLAB regardless of its operating system. Script files must have an extension of ".m" and be in a directory that MATLAB knows about.

MATLAB scripts interact with the current MATLAB environment. Variables set before the script is executed can affect what happens in the script. Variables set in the script remain after the script has finished execution.

One way to edit MATLAB scripts is to use the built-in editor. The editor has some features that make editing MATLAB scripts easier. The editor is integrated with the debugger which makes finding and correcting errors in your scripts easier. More detailed information about editing scripts can be found at Mathworks Matlab Tutorial-Creating Scripts with MATLAB Editor/Debugger.

Use comments to remind you and help other users understand how you have implemented your program. Comments begin with the character %.

For MATLAB to correctly execute a script, it must know the directory in which the script resides. There are several ways to do this. One is to set the current working directory for MATLAB. Another is to set the search path. More detailed information can be found at Mathworks Matlab Tutorial-Working with Files, Directories and Paths.

M-file names should begin with a letter and only contain letters and numbers. Any other characters (space, dash, star, slash, etc.) will be interpreted by MATLAB as operations on variables and will cause errors. Also, M-file names should not be the same as variables in the workspace, since MATLAB will not be able to differentiate between the file name and the variable.

Content actions

Give Feedback:

E-mail the module author | Rate module ( How does the rating system work?)

Rating system

Ratings

Ratings allow you to judge the quality of modules. If other users have ranked the module then its average rating is displayed below. Ratings are calculated on a scale from one star (Poor) to five stars (Excellent).

How to rate a module

Hover over the star that corresponds to the rating you wish to assign. Click on the star to add your rating. Your rating should be based on the quality of the content. You must have an account and be logged in to rate content.

(0 ratings)

Download:

Add module to:

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections directly in Connexions. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need a Connexions account to use 'My Favorites'.

| A lens (?)

Definition of a lens

Lenses

A lens is a custom view of Connexions content. You can think of it as a fancy kind of list that will let you see Connexions through the eyes of organizations and people you trust.

What is in a lens?

Lens makers point to Connexions materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

Who can create a lens?

Any individual Connexions member, a community, or a respected organization.

What are tags? tag icon

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks