Unformatted text preview:

Lab 1: Introduction to MATLAB (Part I)∗EE299 Winter 2008Due: In lab, on or b e fore 17 January 2008.You will need: a pair of headphones for listening to sound signals.ObjectiveThe purp os e of this lab is to teach you some of the basics of using MATLAB, a popular software package thatis used in many engineering courses and that we will be using for the EE299 labs. It is NOT a c omprehensiveor general introduction to MATLAB—there are plenty of good references for that (see the class web page forlinks to a few). We will focus on how to use MATLAB for manipulating and modifying sounds and images.The four sections of the lab cover:• Getting started with MATLAB, using it interactively;• The concept of a variable, including vector and matrix array variables and how to create, modify andaccess elements of the arrays;• Representing sound signals as vectors, including loading, plotting and playing them; and• Representing images as matrices, including loading, modifying and displaying them.We will cover more MATLAB basics in Lab 1, including writing functions and scripts.Throughout this lab are short exercises that check your understanding of the material. Take notes asyou go – your TA will go over the questions and demonstrations with you at the end of the lab.PreLabReview the document “Using MATLAB for EE students” for information about using MATLAB in the EEcomputer labs, creating and saving files and folders, and cleaning up. See the link on the class webpage orgo to: http://ssli.ee.washington.edu/courses/ee299/EEUsingMATLAB.html.Learn two ways to get help in MATLAB: 1) via the ’help’ menu at the top of the window (organized bytopic), and 2) by typing >> help command when you know the name of a particular command and want tolook up how to use it or details about what it does. (Note that >> is the prompt in MATLAB, and thisfont is used to indicate something specific that you type to MATLAB.)Familiarize yourself the following sections from the “Getting Started” pages of the MathWorks OnlineMATLAB Tutorial: Introduction, Matrices and Arrays, and Graphics. Again, you can use the link on theclass webpage, or go to: http://www.mathworks.com/access/helpdesk/help/techdoc/learn matlab/, or justuse the MATLAB help function. You don’t need to know all of the material covered here – the point is toget a sense of what you can do with MATLAB and to know where to look for more information when youare ready for it.∗Portions of this document have been adapted from the 2006 HKN Matlab tutorial.11 Using MATLABOpen MATLAB. On a windows machine in the EE computer lab, click on:Start > EE programs > MATLAB #where # is the release number (e.g. 7.1). Figure 1 shows what your screen should look like when MATLABis first opened. The primary areas of interest are described briefly below. Figure 1: This is what it should look like when you first open it.Current Directory: This area shows you all files and folders in the “current” folder/directory seen byMATLAB. When you make your own MATLAB files, it’s a good idea to make sure they are in the currentdirectory. If you keep them in a different folder MATLAB may not be able to find them when you try to runthem. You can always change the current directory using the directory pull-down menu. This is describedmore in the “Using MATLAB for EE Students” document.Workspace: This area (which you can access by clicking the tab located under the “Current Directory”area) shows you a list of all variables being used. We discuss variables in the next section.2Command Window: This area allows you to enter commands which will be executed immediately af-ter pressing ENTER. A command is basically an instruction to MATLAB telling it what you want it to do.Each command is automatically proceeded with a >> (or in the Student Edition, a EDU>). Try typing 4+4and press ENTER—you should see ans=8.The Command History window will show the list of commands you typed into the Command Win-dow during this session. You can re-enter previous commands by using the “up” key. Try typing clc. Thisgives you a blank command window but doesn’t clear your history.In the next three sections we will work with MATLAB by entering commands in the Command Window.Each command is done (executed) by MATLAB as soon as you hit ENTER. (Another way to use MATLAB,which we’ll cover in Lab 1, is to save all your commands in an M-file and have MATLAB run them insequence as in a computer program.)2 VariablesA variable is like a named storage container. Variables can store numbers, set of numbers, and other thingslike vectors and matrices, which we’ll learn about later. The following command creates a variable called“bob” and stores in it (or assigns it) the value 3:>> bob=3A variable in MATLAB can have any name you want, made up of letters, numbers, and underscore ( ),as long as it starts with a letter. Variables are a ge neral programming concept. If you are familiar withother programming languages like C or Java, you know you need to “declare” variables before assigningvalues to them, but in MATLAB you do not. As soon as you type bob=3, the variable bob gets created withthe value 3. You can later re-assign to bob a different value, in which case the previous value gets overwritten.Type the command: a=bob+4. MATLAB will print: a=7. The statement a=bob+4 does two things:(1) performs the calculation bob+4, which in this case is 3+4(2) creates the variable a (if it does not yet exist) and assigns to it the result of 3+4You can store a new value in the variable a just by assigning it again.Exercise 1: After the com mand a=bob+4, type the c ommand>> a=a+2What value does the variable a have now? Why?As you start to work on more interesting problems, you will be using many variables. You can view yourlist of variables (names, current values, and classes) in the Workspace, or by typing >> whos.Some good things to know about variables in MATLAB:• When you enter a value but do not assign it to anything, MATLAB assigns it to a “default” variablecalled ans. This is why >> 4+4 results in MATLAB printing ans=8.• Certain values are already assigned (“built-in”) to MATLAB—for example, a truncated version of πis contained in the variable pi.• When you quit MATLAB, all variable assignments go away. If you want to clear the values while youare still in MATLAB, type clear. If you want to clear only certain ones, types clear variable names.32.1 Arrays,


View Full Document

UW EE 299 - MATLAB

Download MATLAB
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view MATLAB and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view MATLAB 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?