Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 1

Task 1.4 Detail: Create a work folder; use a script to execute simple commands; save the script to the work folder.

Summary of new tools and commands.

Task: Create and save a script to the current folder

MATLAB always has a current work folder where it expects to find command scripts and other files and where it saves new scripts that are created. MATLAB uses the Documents folder by default (but you should set up your own choice).

It is to your advantage to organize your work into your own folders. You should separate this MATLAB class work from your research efforts (and scripts used for other classes).

The current work folder is displayed in the menu bar just above the command window pane. You can use the icons on the left side of this bar to move around your file system. You can use the browser icon to move quickly around your file system. It also has the option of creating a new folder.

For users with experience using UNIX, the command cd (meaning "change directory") will be a familiar way to change the working directory. In the command window, you can type cd MatClass to move to the MatClass folder (assuming it is a subdirectory in your current working folder). Type cd .. to go up one directory level (go to the parent of the current directory).

MATLAB remembers your various work folders, so the next time you want to work in the class folder, it will appear in the dropdown menu on the right side of the current folder bar.

     matlab window

Create a new work folder for the MATLAB class and make it your work folder.

Use the "new script" button on the MATLAB window to create a command script. Save it with the name "class1.m". Enter the commands as indicated below which define a couple of variables, perform arithmetic on some of them and evaluate two trig functions.

     class 1

Variable names can use letters and numbers as well as underscore "_". Variable names must start with a letter and they are case sensitive. Names cannot include any spaces. In the script above, S and s are two different variables.

Be sure to use descriptive names for variables (I have not done that here). There is no penalty for longer names so make the variable name meaningful.

The arithmetic operators have the meaning that you might expect with * indicating multiplication. The ^ symbol is the power or exponent operator, so that a^2 is a squared or "a*a"

We will find out that some variable names are already defined and that arithmetic operators *, /, and ^ have special powers and need to be used carefully. They are designed to work on vectors and matrices (from linear algebra) which we will find out about in the next class.

Creating large and small numerical values can be done with a scientific notation in the form B = 1.234e5. The e5 part of the number means to multiply the number by 10 raised to the fifth power. V = 12.34e-5 allows specficiation of small numbers (divide by 10 raised to the 5 power). D = -123.4e-5 is a small negative number. The e in the number can be either upper or lower case.

Use the "run" button on the editor to execute this script (and save the current version of the script). You will notice that class1 appears on a line in the command window. The workspace now has these six variables listed along with their values.

The first line in the script starts with the percent symbol % which is the "comment" character for MATLAB. All characters on the line after % are ignored. This allows you to put comments in your script to indicate what commands do, what units are used, what variables represent, and so forth. Be free with your comments; there is no penalty for using them. As your scripts become more complicated, you will need these comments to remind yourself what you are trying to do.

You will be surprised how quickly you forget what you were doing in various scripts. If you ever think "this is a good/clever idea", it is the appropriate time to add a comment explaining your idea.

The ; character at the end of the lines is not required but it is useful. It suppresses the echo printing of the variable name and its value (as we saw in the earlier script). Using this character at the end of lines is recommended to avoid filling the command window with bunches of characters.

One way to print the value of a variable is to type the variable name without the ;. There are more graceful ways to do this as we will see in a bit.

Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 1


email: J. Klinck