DOC PREVIEW
Rose-Hulman ECE 300 - MATLAB Scripts and Functions

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

ROSE-HULMAN INSTITUTE OF TECHNOLOGY Department of Electrical and Computer Engineering EC 300 Signals and Systems Fall 2005 RDT MATLAB Scripts and Functions Lab 2 Mark A. Yoder and Bruce A. Ferguson Objectives The purpose of this lab is to learn to write program files in MATLAB. While working through this lab you should learn many of the commands we will be using throughout the quarter. In addition, you will learn to create MATLAB programs, which we will use, reuse, and modify throughout the term. Use the help command often and early. Specifically, you should achieve the following: • Learn the purpose of MATLAB scripts and functions (aka “m-files”) • Learn to create a script file for performing repeated operations • Learn to create functions for custom operations • Learn to properly graph data from various sources Pre-Lab 1. Create a working directory for you MATLAB work in this class. You should create a MATLAB directory with folders for ECE 300 Class and ECE 300 Lab. 2. Read the following notes, and write a good definition of the “path” function/variable used in MATLAB. Type helpwin path for details about the current path setting. 3. Indicate how directories are added to the path. Add the directories you have created to the path variable. Enter this information into your lab notebook, and turn a photocopy in as instructed. Notes on Using MATLAB MATLAB can be used in two different ways – through the command line interface and using MATLAB scripts. This second lab focuses on the use of MATLAB programs. The second method of interfacing is by writing MATLAB programs called scripts or “m-files”. These are programs written in MATLAB’s command language, the same as is used in the command window. In the last lab, you typed a sequence of commands in a specific order to achieve some more complex result (such as plotting sinusoids). Each command had a specific purpose, and the order was important. However, one big problem with the method used in that lab was that you could not save the sequence of commands that you used. MATLAB programs allow you to save a sequence of commands for reuse and modification. The usual structure of a MATLAB program is similar to that of other programming languages: a main program is written (filename.m), and this program can call functions saved in other files (function_name.m). The main program is where you organize your work into a coherent flow. Functions are used to program repeated command sequences, so that your main program can be Page 1 of 8EC 300 Signals and Systems Fall 2005 neater and smaller. Every command you type in the command line interface window is actually a MATLAB function. Both the main program and functions are called m-files, but they have slightly different structures. It is suggested that you use the MATLAB script editor to create and modify your m-files. The editor can be accessed from inside MATLAB (“File → New → M-File”). Be sure to save the file before you run it. If you do not, MATLAB will use the previous version of the file which it has stored in memory. Simply type the filename (without the “.m”) from the command line to run the program. MATLAB references the script (program) only by its filename, so choose a name that has meaning. (Seventeen files named “testx” make for a confusing lab exercise!) The files you create and save are stored in the directory shown in the “current directory” window just above the command window. Be sure to create a directory for your ECE 300 labwork, and navigate to that directory using the browse button (ellipses - “…”) to the right of the current directory window. MATLAB can access any file stored in its path variable. The path is a list of directories that MATLAB has been told to look into to find commands and files. When you type a command burgerflip in the command window, MATLAB looks through its path to find a file named “burgerflip.m”, and then executes it. Finally, when you save a function, MATLAB will know the function only by its filename. Even if your function definition in the first line of the m-file has something different listed as the command name, the filename in which you store the function is MATLAB’s official reference. So when you call the function from either the command window or an m-file, you must use the name of the file the function is stored in. It is obviously good practice to make both the file name and the function definition line use the same name. Procedure: There are three exercises for today’s lab 1. Script file basics (a) Use the MATLAB editor (“File → New → M-File”) to create a program file called sintest.m containing the following lines: t = -2:0.05:3; x = sin(2*pi*0.789*t); % plot a sinusoid plot(t,x), grid on title(‘Test Plot of Sinusoid’) xlabel(‘Time (s)’); Save the file. (MATLAB cannot access your programs or functions until they are saved from the editor.) Page 2 of 8EC 300 Signals and Systems Fall 2005 Creating a program file in this fashion allows for easy storage of a number of commands as a MATLAB program. The program file is also called an M-File or a script. The program will be stored in the current directory indicated in the MATLAB window. Run your function from MATLAB by typing its name (sintest) at the MATLAB prompt (note that sintest must reside in MATLAB’s search path). Verify that the program produces the expected results. (b) You have now created your first MATLAB program! Type type sintest at the command prompt to see the file you have created. (c) Edit sintest.m to add a line containing the “hold on” function (type help hold for more information), followed by another plot command to add a plot of 0.5*cos(2*pi*0.789*t) to the plot created above. Add a final line to your program containing “hold off”. Note that you must save sintest.m in order for MATLAB to access the updated program. Instructor Verification (see last page) 2. MATLAB Functions MATLAB commands typed at the command line interface are simply functions. In this exercise we will learn to create various types of functions. (a) Create a simple function with no arguments. Type the following lines and save them in a file called greeting.m. Save the file, and test it at the command line. This function is just a small program similar to your main program, with one major difference. function greeting disp


View Full Document

Rose-Hulman ECE 300 - MATLAB Scripts and Functions

Documents in this Course
Exam 2

Exam 2

8 pages

Load more
Download MATLAB Scripts and Functions
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 Scripts and Functions 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 Scripts and Functions 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?