Starting MATLAB - IPS



Index

Index 1

Starting MATLAB 2

Running the Demos 4

Getting Help 7

Creating Variables 12

Performing Calculations 16

Visualizing Data 26

Creating Scripts with MATLAB Editor/Debugger 35

Introduction 35

A Simple Script 35

Running Scripts 36

Note: 37

The MATLAB Editor/Debugger 37

Saving Variables and Sessions 39

Working with Files, Directories and Paths 45

Starting MATLAB

You can start MATLAB by double-clicking on the MATLAB icon or invoking the application from the Start menu of Windows. The main MATLAB window, called the MATLAB Desktop, will then pop-up and it will look like this:

|[pic] |

| |

You will notice that inside the MATLAB command window is the text:

To get started, select "MATLAB Help" from the Help menu.

[pic]

The "[pic]" is called the command prompt, and there will be a blinking cursor right after it waiting for you to type something. The idea is that you type commands at the command prompt for MATLAB to execute; after you type a command at a command prompt, MATLAB executes the command you typed in, then prints out the result. It then prints out another command prompt and waits for you to enter another command. In this way, you can interactively enter as many commands to MATLAB as you want.

Later lessons will go into a lot of detail about particular MATLAB commands, but for now, just to get you started, let's enter a simple MATLAB command, the date command. Click the mouse where the blinking cursor is and then type date and hit the enter key. MATLAB should then return something like

[pic]date

ans =

01-Jan-2008

where the current date should be returned to you instead of 01-Jan-2008. Congratulations! You have just successfully executed your first MATLAB command!

Another simple command you can try now is the clc command (clear command window). In the MATLAB command window, if you want to clear away all the visible text and have the cursor move to the top of the window, then type clc at the command prompt. Go ahead and try it now to see what happens. That was simple, wasn't it?

One final note, when you are all done with your MATLAB session you need to exit MATLAB. To exit MATLAB, simply click the mouse on the File menu of the MATLAB command window and then select "Exit MATLAB" (alternatively, you could just enter quit at the MATLAB command prompt).

Running the Demos

You should have already started MATLAB, and have the MATLAB command window open on the Windows desktop. If not, go back to the previous lesson to find out how.

Well, we're trying to get you started with MATLAB here, so why don't we try some demos.

There are actually many different demos, all accessible from a demo window that is popped up when you type, demo, at the command prompt.

You are not really expected to learn the details of using MATLAB through these demos, but rather just to get a good feel for the kinds of things you can use MATLAB for. There are even some games you can play in these demos, and most of the demos have quite visually appealing graphics, so you should enjoy going through these demos. MATLAB is a very practical and powerful tool for solving many different types of problems, and you should appreciate this more after going through some of the demos.

So, to start the MATLAB demos type, demo, at the command prompt and then hit the enter key. The MATLAB demo window should then pop-up and wait for you to choose and run the various demos; the MATLAB demo window looks like this:

|[pic] |

| |

We won't go through all the demos here in this tutorial, but rather get you going by showing you how to start one of them. You can then work through some of the others similarly at your own pace.

To see the different demo categories click on the plus sign (+) next to MATLAB in the Demo tab of the Help Navigator. You should see the folders of Matrices, Numerics, Graphics, etc. Within each of these categories are several individual demos.

The text box on the right side contains a description of the currently selected demo category; For example if you select Gallery as the demo category, the top right text box will contains a brief description of this category (i.e., "Gallery - This virtual Gallery is for displaying...").

You can see the different demos associated with a given category by expanding the folder (click on the plus sign next to the category); so within the Gallery category, there are individual demos named Knot, Quiver, Klein II, etc. If you select an individual demo some information regarding the demo will appear on the right hand frame. And two of the options that you have are to run the demo, and to view the code for the demo.

As an example try the following. Select Gallery within your own open MATLAB demo window. Then, select the demo Slosh and run it. You should get the following window to pop up:

|[pic] |

|. |

This demo simply shows you another good example of the kinds of graphical effects you can achieve with MATLAB; some of the other demos are more involved, requiring you to click buttons to navigate through them and make selections.

Take some time now and run some of the other demos to get a feel for the possibilities of MATLAB.

Before moving on, you might also want to run the intro demo. This is another example which will give you a basic introduction to MATLAB and its capabilities. To run the intro, select MATLAB->Matrices->Basic Matrix Operations under the demos section of the Help Navigator, or type, playshow intro, at the command prompt. Another window to pop-up, and you can then click buttons on this window to move through the demo.

Getting Help

Introduction

MATLAB has an extensive help system built into it, containing detailed documentation and help information on all of the commands and functions of MATLAB. This help system is extremely useful for beginners to MATLAB, but even after becoming an expert at MATLAB, you will still use the help system to learn about other MATLAB functionalities that you have not yet used before. This lesson will show you how to use the MATLAB help system.

Getting Help From The Command Line

There are three main functions that you can use to obtain help on a given function: help, helpwin(short for help window) and doc (short for documentation). The functions help and helpwin give you the same information, but in a different window, the doc command returns an HTML page with a lot more information.

Below is the output of getting help on the date function, using the different functions.

[pic]help date

DATE Current date as date string. S = DATE returns a string containing the date in dd-mmm-yyyy format.

See also NOW, CLOCK, DATENUM.

Note: The output of help also refers to other functions that are related. In this example the help also tells you, See also NOW, CLOCK, DATENUM. You can now get help on these functions using the three different commands as well.

[pic]helpwin date

|[pic] |

| |

Note: Though the content information from helpwin and help are the same there are advantages to using helpwin over help:

- The text is displayed in a separate window.

- You can just click on the functions listed under "See also" rather than having to type it in at the command prompt to view its help information.

- The Default Topics link will list all the function categories of MATLAB so that you can see the functions associated with a given category. For example, if you wanted to know what functions exist for plotting X verses Y you can click on the matlab\graph2d topic link.

[pic]doc date

|[pic] |

Note: The output of the doc command is the most complete. It not only breaks the information into sections, but also offers complete examples that can be studied and executed.

Try using the different help commands on the following functions: magic, fft, and surf.

Using the MATLAB Help Browser

Another source of help is the MATLAB help browser. You can invoke the MATLAB help browser by typing, helpbrowser, at the MATLAB command prompt, clicking on the help button [pic], or by selecting Start->MATLAB->Help from the MATLAB desktop.

|[pic] |

| |

The help browser has two main sections the Help Navigator, and the display pane. The display pane, as the name implies, displays the information selected. The Help Navigator is on the left hand side of the help browser, and should be used to navigate through the online help information. It includes the following things:

• Product filter - Set the filter to show documentation only for the products you specify.

• Contents tab - View the titles and tables of contents of documentation for your products.

• Index tab - Find specific index entries (selected keywords) in the MathWorks documentation for your products.

• Demos tab - View and run demonstrations for your MathWorks products.

• Search tab - Look for a specific word or phrase in the documentation. To get help for a specific function, set the Search type to Function Name.

• Favorites tab - View a list of links to documents you previously designated as favorites.

|[pic] |

Of all these functionality the two most useful are the Contents tab and the Search tab. As noted above the Contents tab can be seen as the table of contents of the documentation. It is a great resource if you are looking to learn more about a given topic. The Search tab searches through all the documentation very efficiently, and also highlights the keywords that you are searching for in the document so that it is easier to read.

Try searching for help on the help browser itself.

Creating Variables

Introduction

Variables are a fundamental concept in MATLAB, and you will use them all the time. Basically, a variable is a holding place for a value which you can give a name to. The point of this is that, when calculating something new later, you can use the value that a variable refers to as part of the new calculation. Going through some examples will make this clear, so let's do that now.

In its simplest mode of use, MATLAB can be used just like a pocket calculator. For example, here is how you would do some simple, calculator-like operations with MATLAB:

[pic]4 + 10

ans = 14

[pic]5 *10 + 6

ans = 56

[pic](6 + 6) / 3

ans = 4

[pic]9^2

ans = 81

The ans Variable

As you can see, MATLAB supports all the basic arithmetic operations: +, -, *, /, ^, etc.; and you can group and order operations by enclosing them in parentheses. However, what exactly is ans above? In short ans is short for "answer", and is used in MATLAB as the default variable name when none is specified. You can refer to that value by just typing ans:

[pic]4 + 5

ans = 9

[pic]ans

ans = 9

However, if a new equation is entered the value of ans would change.

[pic]ans + 10

ans = 19

Defining Your Own Variables

The ans variable by itself isn't that useful, but the real power is that you can define and use your own variables. For example:

[pic]a = 10

a = 10

[pic]b = 20

b = 20

[pic]c = 30

c = 30

[pic]a

a = 10

[pic]the_average = (a + b + c) / 3

the_average = 20

Listing Currently Defined Variables and Clearing Variables

Let's say you have defined a lot of different variables. You probably can't remember all the variable names you have defined, and so it would be nice to get a list of all the variables currently defined. This is exactly what the whos command does. Simply typing whos at the command prompt will return to you the names of all variables that are currently defined. For example:

[pic]clear % clear variables from memory - see notes below

[pic]a = 5

a = 5

[pic]b = 6

b = 6

[pic]whos

|Name |Size |Bytes |Class |

|a |1x1 |8 |double array |

|b |1x1 |8 |double array |

Grand total is 2 elements using 16 bytes

Once you are done with the variables that you have defined how do you remove them from memory? This is exactly what the clear command is for. Typing clear at the command prompt will remove all variables and values that were stored up to that point. For example, continuing from the above example:

[pic]whos

|Name |Size |Bytes |Class |

|a |1x1 |8 |double array |

|b |1x1 |8 |double array |

Grand total is 2 elements using 16 bytes

[pic]clear

[pic]whos

Suppressing Results with Semicolons

Semicolons typed after commands can be used to hide the printing out of results. If you type an expression (such as "b = 4 + 5") and then follow it with a semicolon, then MATLAB will evaluate the expression and store the result internally, but it will not print out the results in the MATLAB command window for you to see. For example:

[pic]a = 10;

[pic]b = 20;

[pic]c = 30;

[pic]d = 40;

[pic]the_average = (a + b + c + d) / 4

the_average =

25

[pic]the_average;

[pic]b

b =

20

[pic]e = 50

e =

50

[pic]the_blank_average = (a + b + c + d + e) / 5;

[pic]the_blank_average

the_blank_average =

30

This might not seem to be very useful, but it is actually quite handy and used all the time. You will mainly be concerned only with some final result in your MATLAB sessions, which will be calculated by combining many temporary, intermediate variables. And by appending a semicolon to the expressions that assign values to the temporary, intermediate variables causes their results to not be printed. For example, in the above example semicolons were typed after the definitions for the variables a, b, c, and d; only the final result, the average value of these four variables, was important and a semicolon was thus not added after the expression for the_average variable, causing its result to be printed.

Variable Names and Assigning Strings to Variables

There are some specific rules for what you can name your variables, so you have to be careful.

- Only use primary alphabetic characters (i.e., "A-Z"), numbers, and the underscore character (i.e., "_") in your variable names.

- You cannot have any spaces in your variable names, so, for example, using "this is a variable" as a variable name is not allowed, but "this_is_a_variable" is fine (in general, you can use the "_" character wherever you would use space to string words together in your variable name).

- MATLAB is case sensitive. What this means for variables is that the same text, with different mixes of capital and small case letters, will not be the same variables in MATLAB. For example, "A_VaRIAbLe", "a_variable", "A_VARIABLE", and "A_variablE" would all be considered distinct variables in MATLAB.

You can also assign pieces of text to variables, not just numbers. You do this using single quotes (not double quotes --- single quotes and double quotes have different uses in MATLAB) around the text you want to assign to a variable. For example:

[pic]some_text = 'This is some text assigned to a variable!';

[pic]some_text

some_text =

This is some text assigned to a variable!

You will mostly just use text when assigning labels to plots (see the plotting lesson to learn about this), but you should be careful not to mix up variables that have text values with variables that have numeric values in equations. If you do this, you will get some strange results. For example, the variable "b" in the following MATLAB session is really a text string, and you get strange results if you try to use it as a number:

[pic]a = 5;

[pic]b = '5';

[pic]a/b

ans =

0.0943

[pic]a*b

ans = 265

Performing Calculations

Introduction

In this lesson, you will be introduced to some basic math concepts, and their corresponding MATLAB data structures. You will see how these data structures can be 1) created in MATLAB, and how they can be 2) index and 3) used.

MATLAB, the MATrix LABoratory

Three fundamental concepts in MATLAB, and in linear algebra, are scalars, vectors and matrices.

A scalar is simply just a fancy word for a number (a single value).

A vector is an ordered list of numbers (one-dimensional). In MATLAB they can be represented as a row-vector or a column-vector.

A matrix is a rectangular array of numbers (multi-dimensional). In MATLAB, a two-dimensional matrix is defined by its number of rows and columns.

Note: Though matrices can have more than two-dimensions, in this lesson we work only with two-dimensional matrices. If you need to work with matrices that have more than two-dimensions, you can refer to the help information of MATLAB.

In MATLAB, and in linear algebra, numeric objects can be categorized simply as matrix: Both scalars and vectors can be considered a special type of matrix. For example a scalar is a matrix with a row and column dimension of one (1-by-1 matrix). And a vector is a one-dimensional matrix: one row and n-number of columns, or n-number of rows and one column.

All calculations in MATLAB are done with "matrices". Hence the name MATrix LABoratory.

Creating Matrices in MATLAB

In MATLAB matricies are defined inside a pair of square braces ([]). Punctuation marks of a comma (,), and semicolon (;) are used as a row separator and column separator, respectfully.

Note: you can also use a space as a row separator, and a carriage return (the enter key) as a column separator as well.

Below are examples of how a scalar, and a vector can be created in MATLAB.

[pic]my_scalar = 3.1415

my_scalar = 3.1415

[pic]my_vector1 = [1, 5, 7]

my_vector1 =

|1 |5 |7 |

[pic]my_vector2 = [1; 5; 7]

my_vector2 = 1

5

7

You will note that one vector is represented as a row vector (my_vector1), and the other as a column vector (my_vector2).

Now you know how to create scalars and vectors, but what about a two dimensional matrix? For example, how do we create a matrix called my_matrix with the numbers 8, 12, and 19 in the first row, 7, 3, 2 in the second row, 12, 4, 23 in the third row, and 8, 1, 1, in the fourth row?

[pic]my_matrix = [8, 12, 19; 7, 3, 2; 12, 4, 23; 8, 1, 1]

my_matrix =

|8 |12 |19 |

|7 |3 |2 |

|12 |4 |23 |

|8 |1 |1 |

With the above line of code, my_matrix is now defined as the 4-by-3 matrix (4 rows and 3 columns).

You can also combine different vectors and matrices together to define a new matrix. But remember that the output needs to be a valid rectangular matrix. Note that the row separator and column separator function in the same way.

[pic]row_vector1 = [1 2 3]

row_vector1 =

|1 |2 |3 |

[pic]row_vector2 = [3 2 1]

row_vector2 =

|3 |2 |1 |

[pic]matrix_from_row_vec = [row_vector1 ; row_vector2]

matrix_from_row_vec =

|1 |2 |3 |

|3 |2 |1 |

[pic]column_vector1 = [1;3]

column_vector1 =

|1 |

|3 |

[pic]column_vector2 = [2;8]

column_vector1 =

|2 |

|8 |

[pic]matrix_from_col_vec = [column_vector1 column_vector2]

matrix_from_col_vec =

|1 |2 |

|3 |8 |

[pic]my_matrix = [8, 12, 19; 7, 3, 2; 12, 4, 23; 8, 1, 1]

my_matrix =

|8 |12 |19 |

|7 |3 |2 |

|12 |4 |23 |

|8 |1 |1 |

[pic]combined_matrix = [my_matrix, my_matrix]

combined_matrix =

|8 |12 |19 |8 |12 |19 |

|7 |3 |2 |7 |3 |2 |

|12 |4 |23 |12 |4 |23 |

|8 |1 |1 |8 |1 |1 |

From these examples you can see how any type of matrix can be created using the square braces, in conjunction with the row and column separators. And with this you can create the data set needed to compute with or visualize with.

More than not, the type of data that you will work with will be vectors. For example you may be given the temperature data for each hour of the day (a vector with 24 elements), and need to plot the data against time. But how would you create the time data? You could create it manually,

[pic]time = [0, 100, 200, 300, 400, ..., 1900, 2000, 2100, 2200, 2300];

or you could use the colon operator. The colon operator allows you to create an incremental vector of regularly spaced points by specifying:

start_value:increment:stop_value

In this case,

[pic]time = 0:100:2300

Instead of an incremental value you can also specify a decrement as well. The following line of code would create a time vector from 2300 to zero with decrements of a 100.

[pic]time_dec = 2300:-100:0

Notes:

1. In the next lesson you will learn how you can actually plot the data.

2. If you want to load in your data from an external source perform a search on "import data" in the search tab of the MATLAB Help Browser.

Indexing Into a Matrix

Once a vector or a matrix is created you might needed to extract only a subset of the data, and this is done through indexing.

To index into an ordered list, a vector, you need to know where the order starts. In a row vector the left most element has the index of one. In a column vector the top most element has the index of one.

Let’s look at the example of my_vector1 and my_vector2 and see how we can index into its values.

[pic]my_vector1 = [1 5 7]

my_vector1 =

|1 |5 |7 |

[pic]my_vector2 = [1; 5; 7]

my_vector2 =

|1 |

|5 |

|7 |

[pic]my_vector1(1)

ans = 1

[pic]my_vector2(2)

ans = 5

[pic]my_vector1(3)

ans = 7

[pic]my_vector2(1)

ans = 1

[pic]my_vector2(2)

ans = 5

[pic]my_vector2(3)

ans = 7

The process is much the same for a two-dimensional matrix. The only difference is that you would specify the row and column index rather than the single index, as we did for the vectors.

[pic]my_matrix = [8, 12, 19; 7, 3, 2; 12, 4, 23; 8, 1, 1]

my_matrix =

|8 |12 |19 |

|7 |3 |2 |

|12 |4 |23 |

|8 |1 |1 |

To access the value of 4 you would type in

[pic]my_matrix(3,2)

ans = 4

Note: The row number is first, followed by the column number.

You can also extract any contiguous subset of a matrix, by referring to the row range and column range you want. For example, if mat is a matrix with 5 rows and 8 columns, then typing mat(2:4,4:7) would extract all elements in rows 2 to 4 and in columns 4 to 7. Here is an example:

[pic]

[pic]

[pic]new_mat = mat(2:4,4:7)

[pic]

You can change a number in a matrix by assigning to it:

[pic]new_mat

[pic]

[pic]new_mat(2,3) = 1999

[pic]

You should keep in mind that, since vectors are just kinds of matrices, all the operations you learned above for matrices can also be used for vectors. For example, you can change numbers stored in vectors as above. There are many other aspects of matrices and vectors that we have not gone into here in this introductory lesson. We will go into some more advanced aspects of matrices and vectors in the core lessons, but you should also explore the MATLAB help texts on your own to learn more about how you can use and manipulate matrices in MATLAB.

Element-By-Element Operations and Matrix Operations

Before anything else, let us define what an element is: an element of a matrix is simply one of the numbers stored in the matrix.

For example, if you saw a sentence that said "a row vector of ten elements" or "a ten element row vector", then you would know that this just means a row vector that has ten numbers stored in it. Likewise, if you saw a sentence that said "the 8th element of the vector V", then you would know that this just means the number stored at the 8th position of V (i.e., V(8)).

When reading MATLAB documentation you will often see the expression "element-by-element", and this has to do with operations that are performed on two matrices of the same size to get another, result vector of the same size. This just means that, to get the value of a particular element in the result vector, you perform the operation on the corresponding (i.e., same position) elements in the two vectors.

For example, "element-by-element multiplication" of two vectors [1 2 3] and [4 5 6] would give you [4 10 18].

The element-by-element operators in MATLAB are as follows:

element-by-element multiplication: ".*"

element-by-element division: "./"

element-by-element addition: "+"

element-by-element subtraction: "-"

element-by-element exponentiation: ".^"

Here are some examples of using the element-by-element operators (notice that there is an error when trying to perform element-by-element operations when using a row vector and column vector together):

[pic]a

a = 1 2 3

[pic]b

b = 4 5 6

[pic]c

c = 4

5

6

[pic]d

d = 1

2

3

[pic]a .* b

ans = 4 10 18

[pic]a .* c

??? Error using ==> .*

Matrix dimensions must agree.

[pic]c .* d

ans = 4

10

18

[pic]a .^ b

ans = 1 32 729

[pic]c .^ d

ans = 4

25

216

An additional note about element-by-element operators is that you can use them with scalars (remember, that just means numbers!) and vectors together. For example, say you wanted to multiply every element of a vector by two; you could do that by performing element-by-element multiplication of the number 2 with a vector:

[pic]a = [1 2 3 4 5 6]

a = 1 2 3 4 5 6

[pic]b = a .* 2

b = 2 4 6 8 10 12

And, you could similarly use ".^", "+", and "-" with a vector and scalar. Here are some examples:

[pic]c = a .^ 2

c = 1 4 9 16 25 36

[pic]d = a + 2

d = 3 4 5 6 7 8

[pic]e = a - 2

e = -1 0 1 2 3 4

You might wonder why the element-by-element multiplication and exponentiation operators have "." appended to the front of them, while the element-by-element addition and subtraction operators do not. The reason is that there are other kinds of multiplication, division, and exponentiation operators for matrices, which are not element-by-element, that are denoted by "*" , "/"and "^".

This brings us to the other type of operations: matrix operations. Element-by-element operations allow us to compute things on an element-by-element basis, but matrix operations allow us to perform matrix-based computation.

For example, the multiplication of two matrices, represented by "*", performs a dot product of the two matrices. What the dot product does is that it first multiplies the corresponding elements (i.e., same position elements) of the two vectors, similar to what element-by-element multiplication does, and then adds up all the results of these multiplications to get a single, final number as the answer. A simple example should make this clear:

[pic]a = [1 2 3]

a = 1 2 3

[pic]b = [4 ; 5 ; 6]

b = 4

5

6

[pic]a * b

ans = 32

To get the answer "32", what MATLAB did was first to perform the multiplications of the corresponding elements of the two vectors: "1*4 = 4", "2*5=10", and "3*6=18". Then, to get the final answer of "32", MATLAB added all these multiplications together: "4+10+18=32".

Putting It All Together

Having gone through this section, you now know how to create your own matrices (using the square braces or the colon operator), index into the matrices, and compute with the matrices (element-by-element computations, and matrix-based computations).

Let’s now look at an example that uses these concepts. Let’s say that you are given the task of finding the solutions to the equation of a parabola (y=x^2). Where "x" is the range from -100 to 100. Once computed, we want to only look at the data from the range -2 to 2.

With what we know we could do the following:

[pic]inc = 1; % you can specify your own value of INC [pic]x = -100:inc:100; [pic]y = x.^2; % compute the square of each element separately

Note: The above is an example of one of MATLAB's powerful features, vectorization: using a vector as an input to an equation, similar to how you can use a variable as input, and get another vector as output which has elements that are the values of the equation evaluated at the numbers in the input vector.

Now, we need to locate the data points from -2 to 2. To do so, we need to know the length of "x", and index into both "x" and "y" at the right locations.

We can either look for a function which will return the length of a vector, or figure it out ourselves. Can you find the function for obtaining a vector’s length? Or are you figuring it out in your head?

[pic]len_x = length(x)

len_x = 201

[pic]midpoint_index = round(len_x/2)

midpoint_index = 101

[pic]new_x_range = x(midpoint_index-2:midpoint_index+2)

new_x_range = -2 -1 0 1 2

[pic]new_y_range = y(midpoint_index-2:midpoint_index+2)

new_y_range = 4 1 0 1 4

Note: This is not the only way to do this example. You could have also found the range using Boolean indexing. Try searching the Help Browser on "Boolean".

Below is another example of vectorization.

[pic]input_points = [-pi : pi/8 : pi]

input_points =

Columns 1 through 7

-3.1416 -2.7489 -2.3562 -1.9635 -1.5708 -1.1781 -0.7854

Columns 8 through 14

-0.3927 0 0.3927 0.7854 1.1781 1.5708 1.9635

Columns 15 through 17

2.3562 2.7489 3.1416

[pic]sine_curve = 3*sin(5.*input_points)

sine_curve =

Columns 1 through 7

0.0000 -2.7716 2.1213 1.1481 -3.0000 1.1481 2.1213

Columns 8 through 14

-2.7716 0 2.7716 -2.1213 -1.1481 3.0000 -1.1481

Columns 15 through 17

-2.1213 2.7716 0.0000

Visualizing Data

Introduction

In the previous lesson, you learned how to create a vector of regularly spaced points, and another vector containing the values of a function at those regularly spaced points. In this lesson, you will learn how you can use such vectors to do basic plotting in two-dimensions and three-dimensions. MATLAB has many advanced plotting features and you can create a great variety of graphs and charts with it. This lesson will only cover the basic aspects of plotting; the core lessons will go into some more detail, and you should also explore the MATLAB help texts for more advanced information about plotting in MATLAB.

Line Plots in Two Dimensions

The most basic plotting command in MATLAB is, surprise, surprise, the plot command. The plot command, when called with two same-sized vectors X and Y, makes a two-dimensional line plot for each point in X and its corresponding point in Y: the numbers in X are on the abscissa (x-axis), and the numbers in Y are on the ordinate (y-axis). In other words, it will draw points at (X(1),Y(1)), (X(2),Y(2)), (X(3),Y(3)), etc., and then connect all these points together with lines.

Let's do a very simple example just to show you exactly what the plot command does. So, let's create two simple vectors in MATLAB:

[pic]simple_x_points = [1 2 3 4 5]

simple_x_points = 1 2 3 4 5

[pic]simple_y_points = [25 0 20 5 15]

simple_y_points = 25 0 20 5 15

Then, to plot these vectors in MATLAB you would type the following at the command prompt:

[pic]plot(simple_x_points, simple_y_points);

MATLAB will pop-up a figure window containing the line plot; it will look like this.

|[pic] |

| |

So, as you can see, it plotted points at (1,25), (2,0), (3,20), (4,5), and (5,15) --- i.e., the points gotten from the two vectors simple_x_points and simple_y_points --- and drew lines between these points.

The ordering of the vectors in the plot command is important, so, for example, if we reversed the order from our simple example:

[pic]plot(simple_y_points, simple_x_points);

then, MATLAB would pop-up a figure window with this plot.

|[pic] |

| |

Note: You can easily print out the line plot by selecting File->Print on the menu at the top of the figure window.

Besides just the basic line plot, MATLAB has commands which will let you add titles and labels to your figures. To add text to a plot, you need to keep the figure window open (i.e., type the commands in the MATLAB command window while the figure window is still open).

The xlabel command allows you to print out a text string describing the x-axis; similarly, the ylabel command allows you to print out a text string describing the y-axis. And, the title command allows you to print out a title for your plot. Finally, to add grid lines to your plot, you can type "grid on" at the command prompt, and grid lines will be added to the open figure window (typing "grid off" will get rid of the grid lines). Here are examples, showing you how to use these commands:

[pic]xlabel('this is text describing the x-axis');

[pic]ylabel('this is text describing the y-axis');

[pic]title('this is text giving a title for the graph');

[pic]grid on;

And, here is how these commands would affect the first plot.

|[pic] |

| |

Let's look at a more practical example of plotting. So, first recall how we could create a vector of regularly spaced points and a vector of function values at those points for some function. Here is how we could do this for the function "y = x^2" (i.e., a parabola) for x values between -5 and 5 and with regular spacing of .1:

[pic]x_points = [-5 : .1 : 5];

[pic]y_points = x_points .^ 2;

It is then a simple matter to plot the x_points against the y_points, and get the familiar plot of a parabola:

[pic]plot(x_points,y_points);

[pic]xlabel('x-axis'); ylabel('y-axis'); title('A Parabola');

[pic]grid on

|[pic] |

| |

Note: The result is very smooth: you can't really see any of the individual line segments like you could for the simple example previously. That is because the points are so close together (at regular spacings of .1) --- MATLAB is still drawing line segments between the points, but your eye just can't see them because they are so small, and so the result seems to be a smooth curve.

Multiple Plots and Subplots

Another thing you might want to do is superimpose multiple plots in the same figure window, to compare the plots for example. This can be done using the hold command. Normally, when you type a plot command, any previous figure window is simply erased, and replaced by the results of the new plot. However, if you type "hold on" at the command prompt, all line plots created after that will be superimposed in the same figure window and axes. Like wise the command "hold off" will stop this behavior, and revert to the default (i.e., new plot will replace the previous plot).

Here is and example of how you can plot several different exponential functions in the same axes (notice how you don't necessarily have to define separate variables for the different functions, but can merely insert the expressions for them directly into the plot command):

[pic]x_points = [-10 : .05 : 10];

[pic]plot(x_points, exp(x_points));

[pic]grid on

[pic]hold on

[pic]plot(x_points, exp(.95 .* x_points));

[pic]plot(x_points, exp(.85 .* x_points));

[pic]plot(x_points, exp(.75 .* x_points));

[pic]xlabel('x-axis'); ylabel('y-axis');

[pic]title('Comparing Exponential Functions');

|[pic] |

| |

Still another thing you might want to do is to have multiple plots in the same window, but each in a separate part of the window (i.e., each with their own axes). You can do this using the subplot command. If you type subplot(M,N,P) at the command prompt, MATLAB will divide the plot window into a bunch of rectangles --- there will be M rows and N columns of rectangles --- and MATLAB will place the result of the next "plot" command in the Pth rectangle (where the first rectangle is in the upper left).

Below is an example of a line plot, a parabola, an exponential, and the absolute value function into four rectangles in the same figure window.

[pic]x_points = [-10 : .05 : 10];

[pic]line = 5 .* x_points;

[pic]parabola = x_points .^ 2;

[pic]exponential = exp(x_points);

[pic]absolute_value = abs(x_points);

[pic]subplot(2,2,1);plot(x_points,line);

[pic]title('Here is the line');

[pic]subplot(2,2,2);plot(x_points,parabola);

[pic]title('Here is the parabola');

[pic]subplot(2,2,3);plot(x_points,exponential);

[pic]title('Here is the exponential');

[pic]subplot(2,2,4);plot(x_points,absolute_value);

[pic]title('Here is the absolute value');

|[pic] |

| |

Line Plots in Three Dimensions

Well, that about covers basic plotting in two dimensions. Let's move on to how you can create basic plots in three dimensions. Here we'll cover two different kinds of three-dimensional plots you can do in MATLAB, 1) three-dimensional line plots and 2) surface mesh plots.

Let's start with three-dimensional line plots, which are analagous to the two-dimensional line plots created with the plot command. The only difference is that the command has a "3" added to it, plot3, and that it requires an extra input, Z, for the third dimension.

Here is a simple example of using the plot3 command, and the resulting output figure window (notice how the all the text labeling commands, including an additional zlabel command, and the grid command can be used just as in the two-dimensional plot command --- you can also use hold and subplot in the same way too):

[pic]X = [10 20 30 40];

[pic]Y = [10 20 30 40];

[pic]Z = [0 210 70 500];

[pic]plot3(X,Y,Z); grid on;

[pic]xlabel('x-axis'); ylabel('y-axis'); zlabel('z-axis');

[pic]title('Pretty simple');

|[pic] |

| |

As you can see, MATLAB plotted points at (10,10,0), (20,20,210), (30,30,70), and (40,40,500) --- i.e., the points gotten from the three vectors "X", "Y", and "Z" --- and drew lines between these points.

Here is a more interesting, but complex, example.

[pic]Z = [0 : pi/50 : 10*pi];

[pic]X = exp(-.2.*Z).*cos(Z);

[pic]Y = exp(-.2.*Z).*sin(Z);

[pic]plot3(X,Y,Z); grid on;

[pic]xlabel('x-axis'); ylabel('y-axis'); zlabel('z-axis');...

title('A little more interesting!');

Note: ellipses --- if you type "..." at the end of a command line, you can hit return and then continue typing the command on the next line).

|[pic] |

| |

Three-Dimensional Surface Mesh Plots

You can use the mesh and meshgrid commands to create surface mesh plots, which show the surface of three-dimensional functions, such as "z = x^2 + y^2". The way it works is that you:

1) Generate a grid of points in the xy-plane using the meshgrid command.

2) Evaluate the three-dimensional function at these points.

3) Create the surface plot with the mesh command.

To generate the grid of points, you make vectors of regularly spaced points for the X and Y axes, then you call the meshgrid command on these vectors to create the grid of points (don't worry about the details, but "meshgrid" basically returns all possible combinations of (x,y) points, where x is taken from X and y is taken from Y, in the form of two matrices --- so what you will see in the generated surface mesh plot will be a bunch of rectangles, of width and length equal to the regular spacing values for X and Y, and the height of the corners of the rectangles will be equal to the value of the function at the rectangles' corner points). For example:

[pic]x_points = [-10 : 1 : 10];

[pic]y_points = [-10 : 4 : 10];

[pic][X, Y] = meshgrid(x_points,y_points);

Then, you evaluate the function at the grid points. Here is an example, using the function "z = x^2 + y^2" (i.e., a three-dimensional parabola):

[pic]Z = X.^2 + Y.^2;

Finally, you create the surface mesh plot, and add any labels you might want.

[pic]mesh(X,Y,Z); xlabel('x-axis'); ...

ylabel('y-axis');zlabel('z-axis');

Here is the resulting plot (notice how the rectangles are of length 4 in the y-axis direction, and 1 in the x-axis direction --- this is because we chose spacing 4 for Y and 1 for X):

|[pic] |

| |

Creating Scripts with MATLAB Editor/Debugger

Introduction

In previous lessons, you learned how to interact with MATLAB in the MATLAB command window by typing commands at the command prompt. And, you saw how you could define and use variables. However, let's say you have a sequence of commands which you find that you use over and over again, in many different MATLAB sessions. It would be nice if you didn't have to manually type these commands at the command prompt whenever you want to use them. This is one of the things that scripts can do for you.

A MATLAB script is an ASCII text file that contains a sequence of MATLAB commands; the commands contained in a script file can be run, in order, in the MATLAB command window simply by typing the name of the file at the command prompt. In this lesson, we will just be introducing scripts with simple examples; however, MATLAB is essentially a general-purpose programming language (like BASIC, C, Java, Pascal, or FORTRAN). So in learning to write scripts, you are basically learning computer programming!

You can use any text editor, such as Microsoft Windows Notepad, or wordprocessor, such as Microsoft Word, to create scripts, but you must make sure that you save scripts as simple text documents (i.e., in the "Save As" dialogue box, choose "Text Document" or its equivalent for "Save as type:"). It will probably just be easiest to create your scripts using MATLAB's built-in text editor, which will be described later in this lesson and which automatically just saves files as ASCII text files for you. Also, when you name your script files, you will need to append the suffix ".m" to the filename, for example "my_script.m". Scripts in MATLAB are also called "M-files" because of this, and the ".m" suffix tells MATLAB that the file is associated with MATLAB.

A Simple Script

So let's look at a simple example of a script that calculates the average of five numbers that are stored in variables. Here is the contents of a script file "average_script.m" that was created with MATLAB's built-in text editor:

% a simple MATLAB m-file to calculate the

% average of 5 numbers.

% first define variables for the 5 numbers:

a = 5;

b = 10;

c = 15;

d = 20;

e = 25;

% now calculate the average of these and print it out:

five_number_average = (a + b + c + d + e) / 5;

five_number_average

You should already understand what the text in black does: it defines the five variables, calculates their average, and then prints out this average. The text in green (i.e., the lines starting with % --- all comment lines must start with %) are comments. Comments are completely ignored by MATLAB during execution, and are intended solely as comments to the user.

Comments are a way for you to document important points about your scripts, so for example, if you put away some script file for a long time and then come back to it later you can be quickly and easily reminded of what it is you were trying to do, and how to use the script; comments are also very useful in helping others to understand your scripts.

Some good advice: make liberal use of comments in your scripts. Note that another good reason to use comments is that the comments you place at the beginning of your scripts (called the header --- the header includes all comment lines up to the first command) will be returned to users when they get help for your script; thus, you should always try to place information about what your script is about and how to use it as comment lines at the beginning of your scripts.

Running Scripts

If the above script "average_script.m" is saved in the present working directory, then it can be run simply by typing average_script at the MATLAB command prompt. Here is the result of running it:

[pic]clear

[pic]whos

[pic]pwd

ans =

D:\Applications\MATLAB6p5\work

[pic]dir

. .. average_script.m

[pic]average_script

five_number_average = 15

[pic]whos

|Name |Size |Bytes |Class |

|  |  |  |  |

|a |1x1 |8 |array |

|ans |1x30 |60 |char array |

|b |1x1 |8 |double array |

|c |1x1 |8 |double array |

|d |1x1 |8 |double array |

|e |1x1 |8 |double array |

|five_number_average |1x1 |8 |double array |

Grand total is 36 elements using 108 bytes

Note:

1) If a MATLAB command is followed by a semicolon the output associated is suppressed.

2) The variables defined in the script remain in the workspace even after the script finishes running, as can be seen by the last whos command above.

The MATLAB Editor/Debugger

Well, now that you know what scripts are and how to run them, let's talk about how you create them with the MATLAB built-in text editor (the MATLAB Editor/Debugger). The MATLAB text editor is similar to other text editors, but it is especially suited for creating MATLAB-specific files, M-files.

There are a number of different features that the MATLAB text editor offer: One features that it offers is syntax highlighting (i.e., comments are green, command lines are black, and other constructs will use other colors), making easier to read the script. Another is that it automatically append the ".m" suffix when saving files, so you don't need to append it yourself. The MATLAB text editor also offers extensive debugging functionality as well (i.e., finding and fixing errors in scripts). In this tutorial we will not go into the debugging functionality of the MATLAB editor, but you can find examples in the Help Browser of MATLAB.

To start the MATLAB text editor simply type, edit, at the command prompt. Another way to invoke the editor is to select File->New->M-file from the MATLAB desktop menu bar. You should see the editor open with a new, empty documents. If you want to start the editor up with an existing file (e.g., a script file you have partially completed), then you can either type at the command prompt, edit, followed by the name of the file, or select File->Open… from the MATLAB Desktop, and then choose the file you want to open from the Open dialogue box. The way you use the MATLAB text editor is essentially the same as how you would use Windows Notepad, or other simple text editors. When opened the MATLAB Editor/Debugger window looks like this:

|[pic] |

That is about all there is to it. Right now, you should practice using the MATLAB Editor/Debugger. Go ahead and start the MATLAB text editor. Below is an example that you can type in to get you started.

% This is my practice script:

% First define 2 variables:

a = 3;

b = 4;

% Then let's assume a and b are the lengths of 2 sides % of a right %triangle and let's calculate the

% length of the other side (the hypotenuse) using the

% Pythagorean formula:

c = sqrt(a*a + b*b);

%and, let's see the result:

c

After you type this text in, save it in your current working directory (we will talk about directories and paths in a later lesson). Then type the name of your script without the ".m" suffix at the command prompt (for example, practice_script) and you should get the output:

[pic]practice_script

c = 5

You might now want to experiment some more with this practice script (for example, add more variables, try different functions such as sin, cos, etc.) To do that, just open your script up in the MATLAB Editor/Debugger again, make your changes, save the file, exit the text editor, and finally type the name of the script at the command prompt to run it. Have fun!

Saving Variables and Sessions

ntroduction

In the previous lesson, you learned how you could type and save a sequence of MATLAB commands in a text file, and run these commands by typing the name of the text file at the command prompt. In this lesson, you will learn how to save all or some of the variables you have defined during a MATLAB session, and then load them in a later MATLAB session (MAT-files). You will also learn how you can save the contents of all or part of the MATLAB command window in an ASCII text file, called a diary file, so that you can view the file in a text editor, or print it out.

Saving Variables

Let's start with saving variables. So, say you have been typing commands and running scripts in the MATLAB command window for a while, and you have defined a lot of variables. You're rushing to finish up your project, but you still have more variables to define and other commands to run before you finish. Unfortunately, you have to get to class in 5 minutes, and so there is no way you are going to finish in one sitting. What you need is some way to save all your variables, and then be able to load them back in after class when you will be able to work on your project again. This is precisely what the save command does for you.

The save command can be used to save all or only some of your variables into a MATLAB data file type called MAT-file. If you want to choose the name of the file yourself, you can type save followed by the filename you want to use. MATLAB will then save all currently defined variables in a file named with the name you chose followed by the suffix ".mat" (for example, if you chose the name my_variables MATLAB would save as "my_variables.mat" in your present working directory). Before saving you should change your present working directory to one of your own directories (such as some directory on your floppy diskette), or specify the complete path to where you want MATLAB to save your variables (for example "a:\my_variables\my_vars"). Let's see some examples of using save:

[pic]clear

[pic]who

[pic]cd c:\my_variables

[pic]pwd % present working directory

ans =

c:\my_variables

[pic]a = 10;

[pic]b = 20;

[pic]c = 30;

[pic]d = sqrt((a + b + c)/pi);

[pic]d

d = 4.3702

[pic]who

Your variables are:

a ans b c d

[pic]save my_chosen_filename

[pic]dir

. .. my_chosen_filename.mat

[pic]clear

[pic]who

The above use of the save command saved all the MATLAB workspace defined variables. If you just want to save some of your variables, you simply list the variables you want to save after typing, save and the filename. For example, here is how you would save only the variables a and c:

[pic]clear

[pic]who

[pic]a = 10;

[pic]b = 20;

[pic]c = 30;

[pic]who

Your variables are:

a b c

[pic]pwd

ans =

c:\my_variables

[pic]save some_of_my_variables a c

[pic]dir

. .. some_of_my_variables.mat my_chosen_filename.mat

[pic]clear

[pic]who

Loading Variables

So now we have seen how we can save all or some of the defined variables. That alone is not very useful unless we can load them back in later to use them again. This is where the load command is used. Typing load followed by a filename (without the ".mat" suffix) will search the MATLAB path (refer to the next lesson regarding the MATLAB path) for the file, "filename.mat", and load all the variables saved in that file (for example, typing load my_vars would cause MATLAB to search for "my_vars.mat" and load the variables saved in it). So, let's see some examples of loading variables back into MATLAB.

[pic]clear

[pic]who

[pic]cd c:\my_variables

[pic]dir

. .. some_of_my_variables.mat my_chosen_filename.mat

[pic]load my_chosen_filename

[pic]who

Your variables are:

a ans b c d

[pic]a

a = 10

[pic]clear

[pic]who

[pic]load some_of_my_variables

[pic]who

Your variables are:

a c

[pic]c

c = 30

You can also choose to load in only some of the variables that are saved in a MATLAB data file (MAT-file). To load only some of the variables saved in a file back into MATLAB, just type the names of the variables you want loaded back in after typing load and the filename (without ".mat") at the command prompt. For example, if variables "a", "ans", "b", "c", and "d" are all saved in a file, then you could use the load command to load only "a" and "c" back in:

[pic]who

[pic]dir

. .. some_of_my_variables.mat my_chosen_filename.mat

[pic]whos -file my_chosen_filename

|Name |Size |Bytes |Class |

|  |  |  |  |

|a |1x1 |8 |double array |

|b |1x1 |8 |double array |

|c |1x1 |8 |double array |

|d |1x1 |8 |double array |

Grand total is 4 elements using 32 bytes

[pic]load my_chosen_filename a c

[pic]who

Your variables are:

a c

[pic]a

a = 10

Saving the Text of MATLAB Sessions

Now let's move on to saving the text of a MATLAB session. To do this, you use the diary command. If you type diary followed by a filename, then MATLAB will append everything you type, and its results (i.e., what MATLAB returns as results) to the file, filename. To stop text from being appended to the file type, diary off, at the command prompt; everything in the command window after, diary off, will not be appended to the file. You can also turn the diary back on by typing, diary on, to start having text appended to the file again.

Here is an example of using the diary command (note closely what was included in the diary file, which is shown below with the "type" command --- for example, note that the command prompt "[pic]" is not included in the diary file):

[pic]cd c:\my_diaries

[pic]dir

. ..

[pic]diary test_diary

[pic]dir

. .. test_diary

[pic]pwd

ans =

c:\my_diaries

[pic]who

Your variables are:

ans

[pic]help sqrt

SQRT Square root.

SQRT(X) is the square root of the elements of X. Complex

results are produced if X is not positive.

See also SQRTM.

Overloaded methods

help sym/sqrt.m

[pic]diary off

[pic]This will not be in the diary file!!!

??? This will

|

Missing operator, comma, or semi-colon.

[pic]clear

[pic]help ans

ANS Most recent answer.

ANS is the variable created automatically when expressions

are not assigned to anything else. ANSwer.

[pic]diary on

[pic]who

[pic]help abs

ABS Absolute value.

ABS(X) is the absolute value of the elements of X. When X is complex, ABS(X) is the complex modulus (magnitude) of the elements of X.

See also SIGN, ANGLE, UNWRAP.

Overloaded methods

help sym/abs.m

[pic]diary off

[pic]dir

. .. test_diary

[pic]type test_diary

dir

. .. test_diary

pwd

ans =

c:\my_diaries

who

Your variables are:

ans

help sqrt

SQRT Square root.

SQRT(X) is the square root of the elements of X. Complex

results are produced if X is not positive.

See also SQRTM.

Overloaded methods

help sym/sqrt.m

diary off

who

help abs

ABS Absolute value.

ABS(X) is the absolute value of the elements of X. When X is complex, ABS(X) is the complex modulus (magnitude) of the elements of X.

See also SIGN, ANGLE, UNWRAP.

Overloaded methods

help sym/abs.m

diary off

Working with Files, Directories and Paths

Introduction

As we have seen there are a number of different files associated with MATLAB that need to be managed, script files (M-files), data files (MAT-files), and diary files. And with so many uses of files in MATLAB, it is important to understand how files are managed, organized, and accessed in MATLAB.

In general, files are managed, organized, and accessed in MATLAB in the same way as in Microsoft Windows, that is, in a hierarchical file system. In fact, you can access all of your Windows files from within MATLAB.

You should already understand how files are organized, managed, and accessed in Windows. However, to review briefly, inside of the computer you are using are one or more disk drives which are named with letters; the primary hard disk is usually named the "C" drive, and the floppy diskette drive is the "A" drive (If you want to see all of your machine's drives, double click on the "My Computer" icon at the top left of your screen). All of your files are stored on these disk drives, and they are organized hierarchically by being stored inside named directories and subdirectories (also called folders and subfolders). For example, in the A drive there might be a directory called "my_stuff" within which is a subdirectory called "letters"; inside "letters" there could be many text files of letters, for example "letter_to_mom.txt" and "information_request.txt". If we wanted to open up the file "letter_to_mom.txt" in some text editor we need to make sure that the text editor knows where this file is located. The way we usually locate and open a file is by using a file opening dialog box, File->Open.

In MATLAB the way that we find a single file to edit is the same as in any other text editor. However, MATLAB needs to do more than just find and edit a single text file. MATLAB needs to be able to locate all the different M-files, MAT-files, etc., and implement them correctly at the same time (i.e. It needs a list of all the paths of where different files live so that they can be accessed.).

How MATLAB Finds Files

Let's get back to the issue of how and where MATLAB finds files. You may already know that MATLAB always look inside your present working directory (type, pwd,at the MATLAB command prompt to see your present working directory) first for any files it needs. However, if the file is not located in the present working directory MATLAB will also search in other directories as well.

These other directories that MATLAB will search through are stored in the MATLAB path; the MATLAB path is simply a list of complete paths to directories which MATLAB searches through to find a given file. Even the present working directory can be thought of as part of the MATLAB path. To print out the current MATLAB path type, matlabpath or path, at the command prompt:

[pic]matlabpath

MATLABPATH

k:\matlab51\toolbox\matlab\general

k:\matlab51\toolbox\matlab\ops

k:\matlab51\toolbox\matlab\lang

k:\matlab51\toolbox\matlab\elmat

k:\matlab51\toolbox\matlab\elfun

.....

.....etc.

.....

k:\matlab51\toolbox\simulink\blocks

k:\matlab51\toolbox\simulink\simdemos

k:\matlab51\toolbox\simulink\dee

k:\matlab51\toolbox\local

In looking for a file MATLAB actually searches first in the present working directory, and then search from the top of the list, to the bottom, until the file is located. If it is not found MATLAB will produce an error message.

[pic]nonexistent_file

??? Undefined function or variable nonexisitent_file'.

So, what do you do if you want to store your MATLAB files in some directory of your choosing, but still have MATLAB be able to find your files? Simple, you add the complete path to your directory to the MATLAB path. There are two ways you can append your own paths to the MATLAB path: 1) use the addpath command - type addpath followed by the complete path to your directory, or 2) use the path tool of MATLAB - type pathtool at the command prompt, or select File->Set Path…

For example, to have MATLAB be able to find files in the directory "a:\my_stuff\letters" you would add it to the MATLAB path.

[pic]type letter_to_mom.txt

??? Error using ==> type

letter_to_mom.txt: File not found.

[pic]addpath a:\my_stuff\letters

[pic]matlabpath

MATLABPATH

a:\my_stuff\letters

k:\matlab51\toolbox\matlab\general

k:\matlab51\toolbox\matlab\ops

k:\matlab51\toolbox\matlab\lang

k:\matlab51\toolbox\matlab\elmat

k:\matlab51\toolbox\matlab\elfun

k:\matlab51\toolbox\matlab\specfun

.....

.....etc.

.....

k:\matlab51\toolbox\simulink\simdemos

k:\matlab51\toolbox\simulink\dee

k:\matlab51\toolbox\local

[pic]type letter_to_mom.txt

Hi mom!

Useful Function When Working With Paths

We have already introduced some useful functions, but below is a more complete list of helpful functions:

pwd - present working directory

dir, or ls - List directory

what - List MATLAB-specific files in directory

cd - Change current working directory

path, or matlabpath - List the MATLAB search path

addpath - Add directory to search path

pathtool - Invoke the path tool interface

help general - List of general MATLAB commands

Below are some examples of there use: [pic]cd c:\my_scripts

[pic]pwd

ans =

c:\my_scripts

[pic]dir

. .. script1.m

[pic]cd a:\my_stuff\letters

[pic]pwd

ans =

a:\my_stuff\letters

[pic]dir

. information_request.txt

.. letter_to_mom.txt

[pic]type letter_to_mom.txt

Hi mom!

Some additional concepts that you may wish to look into in the content tab of the MATLAB help browser:

- Programming and Data Types: M-File Programming: Flow Control

- Programming and Data Types: M-File Programming: Functions

- Graphics: Creating Specialized Plots

- Graphics: Printing and Exporting

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download