# MAS 622j matlab help

Originally written by Tom Minka, and modified by Yuan Qi and Ashish Kapoor Sept. 2002

Matlab on Athena . Matlab variables . Matlab language . Plotting Examples . Useful Subroutines

### Getting started

Open an editor window next to your matlab window. You'll often find yourself mousing text in the editor window and pasting it into the matlab window.

If you're a complete matlab novice, type "intro" or, if you're real fond of splashy colors, type "demo". You can also read help from matlab help menu.

You can also type "helpwin" or "helpdesk" to get a help window. The matlab help command is "help". Try "help general" or just plain "help". The apropos command is "lookfor". Try, e.g. "lookfor tangent" or "lookfor random".

### Syntax

The matlab continuation code is "..." i.e. if you have a long formula that is several lines long, end each line with ... and continue the formula on the next line.

The matlab comment character is "%". The semicolon ";" is useful too; it makes the command on that line operate silently. For example, "A = B;" copies matrix B into matrix A. "A = B" does the same thing, but prints out the whole contents of matrix B while copying.

### Data structures

The most useful data strucutre in Matlab is matrix. Scalars is considered 1 by 1 matrices, and strings (delimited by 'single quotes') are considered vectors (which in turn are just skinny matrices). Check out
`  sprintf  sscanf  num2str  int2str`
Matlab5 and 6 added the very useful "cell array" and "struct" data types. A cell array is just like a matrix except each entry can be any data type, not just a number. For example:
```
>> c = {'joe' 5 [1 2 3]}

c =

'joe'    [5]    [1x3 double]

>> c{3}

ans =

1     2     3```
A struct is similar except its contents are addressable by name only.
```>> s = struct('name', 'joe', 'age', 30)

s =

name: 'joe'
age: 30

>> s.name

ans =

joe

>> s.age

ans =

30```

### Plotting and I/O

Matlab is very good at plotting your data. See our plotting examples and/or get matlab's help on:
```  plot  grid  hold  drawnow  axis  axes  orient
subplot  mesh  meshgrid  plot3  rotate3d```
Then you'll want to print your plot to a file. To print the current plot to a postscript file, type
```  orient tall   %% this line is optional
print -deps myfilename.ps```
You can use C style file I/O. Get help on commands
`  fopen  fclose  fscanf  fprintf  printf`
The matlab parser is not too clever, so don't put "-" in a filename; poor matlab will think you want to subtract!

Matlab has a native data file format, plus it can save and load data from ASCII files. See the stock answers and check out

```  save filename.dat -ascii