When you start MATLAB, you should see a window appear which contains a number of panes, something like this:

Let's look at the various parts of this window:

**Command**- Type your commands to MATLAB here -- for example, the commands to run a function, or to set a variable equal to some value. All the output of your functions will appear in this pane, too.
**Folder**- Use this pane to look for your program files, or to set the folder in which your files will be saved.
**Variable**- As your program executes, the values of variables will appear here. This is very useful in conjunction with the debugger.
**History**- All the commands you type are saved here for future reference. You can also use the up-arrow key in the command pane to recall earlier commands.

There are also several windows which may pop up in response to your actions.

The **plot window**
shows any plots which you may create
with MATLAB's own
*plot* command.

The **editor/debugger window**
allows you to edit MATLAB programs.
You may also use any other text editor
to do so, outside of MATLAB,
if you wish.
However, the editor incorporates the
MATLAB **debugger**,
which is a powerful tool.

There are additional windows inside MATLAB, but we probably won't use them very much.

- MATLAB is interpreted, not compiled (unless you purchase the compiler and learn how to use it)
- Variables
- no need to declare in advance -- in fact,
**can't**declare - no need to specify type
- can switch from one type to another
- assign value with '='
- if type name all by itself, with no semicolon, value printed to screen (in fact, this is true for any expression which doesn't end in semicolon)

- no need to declare in advance -- in fact,
- Workspace
- save it with
**save filename** - restore with
**load filename** - record all commands typed with
**diary filename** - stop recording with
**diary off**

- save it with
- Script files
- place commands into a file
**mycommands.m**(the name doesn't have to end with**.m**, but it may make the file easy to recognize) - type
**mycommands**in the command window, and everything is executed (don't include the ".m" in the name)

- place commands into a file
- Arithmetic
- standard operators
**+ - * /** - exponent operator
**^** - use parantheses to specify order of operations
- there are no C-like
**++**or +=

- standard operators
- Built-in Functions
- lots and lots and lots of 'em --
see
the section on builtin functions on the MATLAB home page
or just type
**help matlab/elfun**for the basic math functions - all trig functions take arguments in radians (not degrees)
and, hooray,
**pi**is pre-defined for you -
**i**is pre-defined to mean "square root of negative 1"; square roots of negative numbers yield complex results - type
**help foobar**to get help on function "foobar"

- lots and lots and lots of 'em --
see
the section on builtin functions on the MATLAB home page
or just type
- Printing values
- Method 1: type the variable or expression without semicolon
- Method 2: use the
**disp**function - Method 3: for maximum flexibility,
use
**fprintf**(sends output to screen or a file), or**sprintf**(sends output to a string variable) - Example: print to screen:
**fprintf(1, 'value of a is %7.1f \n', a)** - note that the format string (like all strings) is defined
by single-quote (apostrophe) characters
**'**, rather than by double-quote (quotation marks) characters**"** - Example: to print to a file,
- open the file:
**fid = fopen(filename, 'w')** - write to it:
**fprintf(fid, 'string %s and integer %d\n', str, int)** - close the file:
**fclose(fid)**

- open the file:

- Expressions -- for scalar quantities (vectors and matrices have
their own set of functions), each of the following
expressions will return
**1 (true)**or**0 (false)**- equal to:
**a == b** - not equal to:
**a ~= b**

this is not the same as the != comparison in C - greater than:
**a > b** - less than:
**a < b** - greater than or equal to:
**a >= b** - less than or equal to:
**a <= b**

- equal to:
- Control Flow
- If
if (a == b) if (a == b) if (a == b) do this do this do this end else elseif (a > b) do that do that end else do the other thing end

- for
for i = 1 : 10 for i = 1 : 0.1 : 2 x = x + i; x = sin(i); end end

- note that the limits are inclusive at both ends, unlike C
- note that if you include the increment, it falls in the middle
of the
**for**statement, unlike C

- while
while (i < 100) while (i < 100) do something do something end if (result > i) break end end

- note that there is no "continue", like in C
- the
**break**command works in**for**loops, too

- If

- The official MATLAB web site includes a great deal of documentation, some of which is in the form of videos.
- There are many university courses which use MATLAB,
so you can find many good tutorials with a little searching.
For example,
- Kristian Sandberg's tutorial from the Department of Applied Mathematics at the University of Colorado
- Edward Neuman's tutorials , from the Department of Mathematics Southern Illinois University

- Wikibooks has an entry on MATLAB programming

Copyright © Michael Richmond. This work is licensed under a Creative Commons License.