## Introduction to MATLAB – Beginners Tutorial

Contents

MATLAB (Matrix Laboratory) is a forth generation high level programming language and interactive environment for numerical computation developed by MathWorks. It allows data analysis and visualisation, matrix manipulations, function plotting, developing algorithms, creating models and applications, interfacing programs written in other languages such as C, C++, Java and FORTRAN. Applications of MATLAB includes Communication Systems, Embedded Systems, Computational Biology, Digital Signal Processing, Computational Finance, Control Systems, Mechatronics, FPGA Design and Co-design, Test and Measurement, Image and Video Processing and Technical Computing.

# MATLAB as Calculator

In the very first step, we can use MATLAB as a calculator… Open the MATLAB by double clicking on the MATLAB icon that should be on the desktop. It will open a window called Command Window.

To add two numbers, for eg: 3 + 7, Type 3 + 7 after fx >> and press Enter.

>> 3 + 7 ans = 10

>> c = 3 + 7 c = 10

If we don’t specify the output variable, it automatically uses the variable ans (short of answer) to store the results of calculations as shown above.

# Using Variables

As in most of the programming languages, equal sign (=) is used for assigning variables as shown below.

First enter a = 5, then b = 9. To find sum of a and b, type a+b and press Enter.

# Clear Screen

*CLC* command can be used to clear screen, type* CLC* and press Enter.

>> clc

# Trigonometric Functions

Trigonometric functions such as sine and cosine are available.

>> d = cos(1) d = 0.5403

>> d = sin(1) d = 0.8415

# Numeric Format

You may have noted in the above example that there are four decimal places. In some applications you may have to change this. You may use one of the several formats available with MATLAB and the default format is called short (four digits after decimal point). You can change Numeric Format from** File >> Preferences**. We can also change numeric formats through commands.

>> format long >> d = sin(1) d = 0.841470984807897

# Numbers

Three types of numbers are used in MATLAB.

- Integers
- Real Numbers
- Complex Numbers

We already deal with Integers and Real Numbers. Commands realmin and realmax can be used to find smallest and largest positive real numbers in MATLAB.

Complex numbers in MATLAB are represented in rectangular system. The imaginary unit √-1 can be represented either using i or j.

>> i ans = 0 + 1.0000i

In addition to these numbers, MATLAB uses Inf, -Inf and NaN to represent non numbers.

- Inf – Positive Infinity (∞)
- -Inf – Negative Infinity (-∞)
- NaN – Not a Number

Infinity is commonly generated by mathematical operations such as division by 0 or by an overflow while NaN is generated as a result of Mathematically undefined operations such as ∞ – ∞ or 0/0.

# Whos and Who

It is a good practice to keep the record of all variables that we have used, since it will occupy our workspace. Sometimes it is a difficult process, MATLAB can do it for us. The command* whos* will display all sort of information about active variables.

>> whos

Another command* who* will provide the names of all the active variables.

>> who

# Clearing Variables

We can erase a variable from memory if it is no longer required by using the command *clear variable_name*.

>> clear ans

To clear all variables use the command without specifying variable name.

>> clear

# Suppressing Output

Sometimes you might not wish to see output at the end of some commands. We can suppress output by putting a semicolon (;) at the end of a command line as shown below.

>> a = 5 + 6; >> a = a - 1; >>a a = 10

# Exit Matlab

You can exit the program using *quit* command. While exiting all variables stored in workspace will be lost and it is not reversible.

>> exit

# Saving and Loading

By using the command *save filename* you can store all the variables in a binary file *filename.mat*

>> save hello

To retrive the information in the binary file we can use the command *load filename*.

>> load hello

Matlab also have the facility to save the entire Matlab session in a ASCII text file. For that we should use the command *diary filename *at the beginning and end of the session. The created text file will include everything that we typed in to Matlab including error messages but it will not include plots.