Lab 0
From CSE100B Wiki
Getting Started
Welcome to your first Matlab class. In this course, we will discover and learn many things. In order to do this, we must first become familiar with the tools we will be using. If you have a question at any time, please ask a TA or the instructor.
- Go to the course web page at http://classes.engineering.wustl.edu/cse100b and bookmark it.
- File-system preparation: please open drive H (Network Drives → 'your username' on warehouse.cec.wustl.edu), and create a new directory called cse100b under My Documents folder (H:\My Documents\). Right click the folder, then choose Send To → Desktop (create shortcut).
Open the folder, and create a new directory called lab0. In the future, create a new directory for every lab in the cse100b directory following the same naming convention. - Create a shortcut for Matlab software by going to Start → Engineering → Matlab 7.0.4 then right-clicking → Send To → Desktop (create shortcut). Now, go to Desktop and double-click the shortcut icon to load MATLAB (it takes some time).
- Familiarizing yourself with the Matlab development environment: When you open Matlab, you will see a screen divided into three windows. They are:
- Command Window - This is where we will type in Matlab commands.
- Command History - When you type a command in the Command Window, it gets remembered here.
- Current Directory and Workspace - The Current Directory shows the working directory. The Workspace displays all the variables that are currently in use.
- Help: Matlab Help is a crucial tool for learning and exploring Matlab. You will become very familiar with this tool. To use this, select Help -> Matlab Help (or press F1).
Note that there are three different ways to access help from here. You can use the Contents, the Index, or the Search tool by clicking on the different tabs. Try all three of these out by looking at a help page for each method. For example, under "Contents", find "Desktop Tools and Development Environment" and read that page, clicking on whatever looks interesting to you.
If you already know the name of a function you're interested in, you can get help by typing
help xwherexis the name of the function.Try this out by typing
>> help help
You can go over the alphabetical listing of functions in the index to search for functions that you are familiar with.
Q1: Write one sentence describing each of the following functions: - clear
- abs
- cos
- sin
- clc
- First experiment: Let's create a variable in the workspace.
Type in the Command Window prompt (that's the line that starts with ">> ") the command:>> a = 23
Notice that the Command History has stored what you typed at the Command Window prompt (one way to retrieve that command is by pushing the up arrow while in the Command Window).
The Workspace now shows that it has a variable named "a". Click on the Workspace tab that is next to the Current Directory tab to verify that the variable has been created.
Now explore the Matlab Development Environment by typing in different commands. You can use common mathematical operators like +,-,*,/ with numbers and/or variables. Watch the Command History accumulate, and the Workspace change, as you type in different commands. Try double-clicking on a variable name in the Workspace to see what happens.
If you have any trouble, ask a TA or the instructor.Q2: What happens when you type:
>> a
Q3: What is the value of
tafter the following command? What does the semicolon "do"?
>> t = 22 / 7;
- Now you've got the basics of using Matlab. Let's learn about another important tool: the Matlab Editor
At the command prompt, we can type one line at a time, but what if we want to perform many commands in succession? We can type all of the commands in the Matlab Editor, save them to a file called a script and run them all by simply typing the name of the file at the command prompt.
Let's create our first script. To open the Matlab Editor, click the empty page symbol under the File Menu (or go to File -> New -> M-File). In the future, you can click on the open file symbol to retrieve a previous m-file (or go to File -> Open...). Now you have a blank document called an M-file to write whatever Matlab commands you like.
Type these commands into the editor:
t = 32
z = 60
r = t - zTo run your script, hit F5 or click the "Run" Button (run your mouse over the buttons to identify their function). Since we have not yet saved our M-file, the editor will first ask to save the file.
- Navigate to your lab0 directory created earlier in the lab.
- Save the file as lab0 (the actual file name will be lab0.m).
- When prompted to change the working directory, say "Yes".
To see the results of running the script (lab0.m), go to the Command Window. Notice that the Current Directory tab includes lab0.m since you changed the current directory. Also notice that the variables t, z, and r are in the Workspace.
Now change the script so that when
lab0is typed into the command prompt (this runs lab0.m), only the value ofris displayed (the rest of the command window should be clear). Be sure to save the script before running it (Ctrl-S, or the editor's "save" button).When you are ready, demonstrate lab0.m to the TA or instructor and answer some questions to checkout. Before you leave, please add your name to the class roster on the People page.
Note
- Naming script or variable: Do follow the naming convention (at least one letter, no special character, etc.). If you accidentally use a keyword or a reserved word as the name for a variable or for your script, Matlab will not *see* your script/variable, since it will see only what the keyword stands for in the system.
- If you ever get the error ??? Undefined function or variable 'lab0'. it means Matlab cannot find what you're referring to (in this case, lab0). Look above the Command Window to see what the current directory is. Matlab is looking in this directory for your file, but it's not there. To fix this, we are going to change the current directory to the directory that contains your file. In the Command Window, type "cd H:\cse100b\lab0" (without the quotes), or use the name of the directory you saved your file in. Note that the current directory displayed above the Command Window now matches what you want.
Wrapping up:
- When you are finished, make sure you log out of your computer by going to Start -> Log Off.
- If you didn't already, make sure your student ID gives you access to the building after hours.
- If you need a CEC account, remember to go to Sever 214 to get one. Please do that now.