# The Basics¶

## Installation and Configuration¶

Instructions for installation from ITS

### User Interface¶

The Matlab window has a bunch of sub-windows.

Of these I only keep 3:

1. file browser: shows files in current directory
2. command window: here you can type commands for interactive calculations
3. editor: here you edit program files
Tip: undock the editor, so it sits in its own window.

### Configuration¶

Options can be set in preferences as usual (keyboard shortcuts, colors, etc).

Options can also be defined in startup.m in the home directory. This is where you want to put any code that should run every time Matlab starts.

You also need a startup file for each project, so that Matlab can find your code.

## Key Features of Matlab¶

Matlab is a mathematical programming language.

Matlab is interpreted (or just-in-time compiled, as opposed to compiled).

This means that it can be used interactively by typing commands at the prompt >> in the command window.

But most of your code will be written to program files (matlab functions).

Functions can be run from the command prompt.

Commands are either built-in or written by the user. Every command is a stored in an M-file.

If you type sum([1,2,3]) at the command prompt, Matlab will look for sum.m and run it. How does Matlab know where to look? It uses the Matlab Path

### Matlab Pros¶

It is easy to learn and use.

• The main reason is that Matlab is dynamically typed. That is, a variable can change type as it is modified.
• Arguments are passed to functions without declaring their types.
• The documentation is outstanding.

Matlab has a large standard library.

• But there is little (high quality) user contributed code.
• Most of it lives on the Matlab file exchange. But this is not curated in any way. Use at your own risk.

### Matlab Cons¶

Relative to compiled languages (such as C) and relative to typed languages (such as Julia), Matlab is slow.

Because the types of function arguments are not checked (unless the user does so), “interesting” errors arise.

## Interactive Matlab¶

In the command window, type your commands at the >> prompt.

Matlab returns the answers on the screen.

Example

>> date
ans =
26-Jul-2015


Typing date at the command prompt executes the built-in date command.

Tip: When you write your own programs, make sure all names are unique. If you write a function called date, there will be trouble.

date returns a string containing the date.

Its return value is shown on the screen after ans =.

To get rid of the ans = part, we could use disp(date).

Example

>> dateString = date;
>> disp(dateString);
26-Jul-2015

Note: Matlab displays the result of any command, unless the command is ended with ;.
If you ever see a lot of numbers appearing on the screen “out of nowhere,” you probably forgot the ; at the end of a line.

##Doing Math

Matlab has built-in operators for common math operations:

a = 2; b = 7;
disp(b*a);
14

y = b ^ a;
disp(y);
49


Built-in functions:

>> sin([1,2])
ans =
0.8415    0.9093


A neat feature: most functions work on arrays of inputs. One command returns the sin values for many numbers in one fell swoop.

## Getting Help¶

To find out more about a particular command, type

help [command name]


To find a command for a given task, use the Matlab help system.

### Matlab tutorials¶

EconPhd

Mathworks

List of free Matlab books

MIT course