# Things you may not know about matlab

Here is a list of some interesting features of Matlab that some are unaware of

## Contents

### Rational representation of results

Consider this example of inverting a 2-by-2 matrix

```>> A = [8 2 ; 1 3];
>> inv(A)
ans =
0.1364   -0.0909
-0.0455    0.3636
```

It is hard to recognize that the elements are decimal approximations of rational numbers. Matlab can help here:

```>> format rat
>> inv(A)
ans =
3/22          -1/11
-1/22           4/11
```

Note: This is, of course, just a setting for how Matlab presents computational results! The internal computaions are carried in floating point.

### Stop if NaN/Inf encountered

A NaN or Inf value in a numerical array is typically a signal that the computation has suffered a breakdown. In some cases it is best to stop and investigate. Matlab can do this.

```>>dbstop if naninf
```

This will cause the debugger to be invoked automatically when a NaN or Inf is encountered during computations.

### fprintf is more powerful than disp

Consider the common situation where one has a loop whith some computation and a printout inside the loop with both the loop counter and the some numerical result. Example that uses disp()

```
>> for i = 1:4, disp([i pi*i]), end
1.000000000000000   3.141592653589793
2.000000000000000   6.283185307179586
3.000000000000000   9.424777960769379
4.000000000000000  12.566370614359172
```

The obvious problem here is that disp does prints with the same decimal output format for both columns. This can be remidied by using fprintf instead.

```>> for i = 1:4, fprintf('%d\t%2.12f\n',i,pi*i), end
1       3.141592653590
2       6.283185307180
3       9.424777960769
4       12.566370614359
```

Note that fprintf works just like it does in C. cplusplus.com has a good reference. Matlab also provides the sprintf function!

### Enter debugger if error

In a similar fashion as above, Matlab can automatically invoke the debugger when an error is thrown. This makes it really easy to quickly inpect the offending lines of code. Just do

```dbstop if error
```

then run the program. Disable this behavior with dbclear if error. Use dbstack, dbup and dbdown to navigate in the call stack.

### Specifying which Java VM to use

On some systems (in particular on Linux), the Java VM that Matlab is either slow or causes crashes. It is possible to let Matlab run on another JVM if desired. This is done by setting the MATLAB_JAVA environment variable. Example

```setenv MATLAB_JAVA /usr/lib/jvm/java-6-sun-1.6.0.03/jre
```

### Execute a Matlab script from the terminal

A matlab script can be executed from the command line directly, with

```matlab -nodesktop -nosplash < prog.m
```

Note that this will start a full Matlab session, execute the script and then exit Matlab. No magic.

### Assert as an alternative to "if then error"

This is an example of a common construction:

```if a < b
error('Fatal error: a is less then b!')
end
```

A more convenient way of writing this uses an assertion instead of the combination of a conditional and a throw:

```assert(a >= b,'Fatal error: a is less then b!')
```

### The Matlab Editor is really useful

... though the full GUI may still be too much. See the Matlab Editor wiki page.