Objectives

- To learn some basics in MATLAB
- To learn to manipulate data in MATLAB
- To learn to import data from other applications, for example
Excel

MATLAB is an interactive system for numerical computations. It is widely used in universities and industry, and has many advantages over languages such as C, Fortran and Java, including:

- It is very easy to write MATLAB code to solve complex problems in mathematics, sciences and engineering.
- Data structures in MATLAB require minimal attention. Arrays, for
example, do not need to be declared.

- MATLAB has high quality graphics and visualization tools that can
be used to analyze computational results.

- MATLAB provides additional toolboxes that are designed to solve
specific classes of problems. For example, there are toolboxes for
statistics, image processing, signal processing, differential
equations, splines and optimization.

MATLAB is very useful for solving problems involving many numbers at the same time. These numbers can be stored as matrices or vectors and computations can be performed on all of them at once rather than one-by-one. We explore some of these basics operations

through a series of examples.

Starting MATLAB

We will use MATLAB in this lab. To start MATLAB, double click on MATLAB icon on your PC. You will get a screen that looks like this:

In this lab manual every time that we show >> we are referring to the MATLAB command prompt where you can type a MATLAB command. Then you have to press Enter to execute that command. MathWorks provides an excellent online introductory tutorial, click here to bring that page up.

Manipulating data in MATLAB

MATLAB gets its name from Matrix laboratory. A matrix is a 2 dimensional array of numbers, with a certain number of rows and columns. For example:

is a matrix with 3 rows and 4 columns. We often say A is a 3 × 4 matrix.

then we say x is a column vector of length 5, and y is a row vector of length 4. MATLAB is very useful for solving problems involving matrices and vectors. We explore some of these basics through a series of examples.

>> x = [1, 2, 3, 4]

This creates a row of 4 values and store them all in x. Thus x is 1-by-4 or 1 × 4 matrix.

>> y = [1; 2; 3; 4]

This one creates a column of 4 values and store them all in y. Thus y is a 4-by-1 or 4 × 1

matrix.

>> y = [1

2

3

4]

This will do the same thing.

Initialization refers to the process of creating a matrix and having some values stored in them right at the beginning.

Initializing Vectors. We can create row and/or column vectors in MATLAB as follows:

>> x = [1 2 3 4]

>> x = [1, 2, 3, 4]

>> x = [1; 2; 3; 4]

Note that the first two produce 1 × 4 matrices and the third is a 4 × 1 matrix. It is very important that you pay careful attention to ";" as that is the separator of rows when you use it. If you do not use that, then you have to use Line-Break to separate the rows.

Initializing Matrices. In general, we can create matrices with more than one row and column just as we did above. Here are some examples:

Here is an example in which we use Line-Break to separate the rows:

>> A = [1 2 3 4

5 6 7 8

9 10 11 12]

In this one which is the same we will use ";" to separate the rows:

>> A = [1 2 3 4; 5 6 7 8; 9 10 11 12]

Array Operations. MATLAB supports certain array operations that can be very useful in scientific computing applications. Some of these operations are:

.* ,
./ and .ˆ

The dot indicates that the operation is to act on the matrices in an element by element way. That is,

Now let's do this in MATLAB:

>> A = [1 ; 2; 3; 4]

>> B = [5 6 7 8]’

Note the (single quote) ’ at the end of the line where B is defined. That is actually a very handy thing that turns a matrix around (transpose). The command

>> B = [5 6 7 8] will create one row of 4 data points. But we really want B to be one column with 4 rows, so if we add the single quote (’) at the end, it will convert that matrix to a column.

Now try

>> A .* B

Note that if these two were not the same type, we couldn't do this operation. In cases such as this one, we say that matrices must be compatible.

Lab Activity (1)

What do you get for each of the following?

>> A .^ 3

>> A ./ B

Initializing Vectors with Many Entries. Suppose we want to create a vector, x, containing the values 1, 2, . . . , 100. We could do this using the colon operator:

>> x = 1:100

Another useful MATLAB command is linspace. In general, linspace(a, b, n) generates a vector of n equally space points between a and b. So, if we want 100 equally spaced points between 0 and 1, you would type the following:

>> n = 100;

>> x = linspace(0, 1, n);

Lab Activity (2)

Create the following two matrix and vector in MATLAB:

Explain what the following commands do and try them to confirm that you were correct. If you have a question feel free to ask:

>> A(1,2)

>> A(:,2)

>> A(1:3,1:3)

>> A(2,:)

>> min(A)

>> max(A)

>> mean(b)

>> max(b)

>> [m,i] = max(b)

>> median(b)

>> help max

>> sum(b)

Importing Excel file to MATLAB

There are a couple ways to import an Excel file to MATLAB. We will demonstrate this using the command xlsread with a sample excel spreadsheet called AprilWeatherBoone.xls. Before we can proceed with this lab activity we need to save the weather file on our PC. To do this right click on AprilWeatherBoone.xls and use the Save Target As option to save it under Desktop directory. Once you saved the file then at the MATLAB prompt, type:

>> num = xlsread(’AprilWeatherBoone’);

Type whos at the command line to learn about the imported date. You should get:

num 30x10 2400 double array

which tells us that the variable num is a 30 × 10 matrix (double array). To see what the values are, just type num at the command line. Here is what you should get for the first 5 columns and first 12 rows (we left off some of the rows and columns due to space). To view the data type::

>> num

num =

63.9000 NaN 52.3000 NaN 7.0000

71.9000 NaN 43.9000 NaN 6.0000

66.8000 NaN 40.8000 NaN 12.0000

49.7000 NaN 31.6000 NaN 24.0000

56.9000 NaN 37.0000 NaN 18.0000

58.0000 NaN 39.9000 NaN 14.0000

75.9000 NaN 51.7000 NaN 4.0000

58.6000 NaN 34.6000 NaN 15.0000

55.7000 NaN 28.2000 NaN 24.0000

61.0000 NaN 28.6000 NaN 18.0000

66.6000 NaN 34.6000 NaN 12.0000

65.4000 NaN 43.5000 NaN 10.0000

If you'd like to read in just part of the spreadsheet, you could type:

>> HighTemp= xlsread(’AprilWeatherBoone’,’A1:A33’)

(To learn more details about xlsread, you could type doc xlsread at the prompt.)

Lab Activity (3)

Use MATLAB to:

1. Find average daily high for the month of April.

2. Find the average daily low for the month of April.

3. Find the total rain fall for April.

4. Find which day had the most rain.

5. Which day in April had the most variability? (Find the day where the high minus the low was the greatest.)