Introduction to Matlab Module 3 Functions Files Topics

Introduction to Matlab Module #3 – Functions & Files • Topics 1. Functions 2. Files (variables, data, spreadsheets) • Textbook Reading Assignments 1. 3. 1 -3. 2, 3. 4 • Practice Problems 1. Chapter 3 Problems: tbd Introduction to Matlab Module #3 Page 1

1) Functions • Functions - Matlab gives you the ability to create your own user-defined functions. - A function is a file that exists that performs a specific task - It is different from a script in that it returns the results of a computation and the result can be assigned to variables. - Variables used within the function file are not seen by the rest of the program (i. e. , they don’t show up in the workspace) so functions can be called without altering existing session variables. - You name the file the same name as the function and give it a *. m extension Introduction to Matlab Module #3 Page 2
![1) Functions • Syntax function [output variables] = function_name(input variables) - “function” is a 1) Functions • Syntax function [output variables] = function_name(input variables) - “function” is a](http://slidetodoc.com/presentation_image_h2/6f0a0dd17cab0b77ba66c51ee8a99672/image-3.jpg)
1) Functions • Syntax function [output variables] = function_name(input variables) - “function” is a keyword and must be placed on the first line of the file - the square brackets represent what variables in the function file will be returned at the function call - “function_name” is user-defined. Your *. m file should have the same name - the parenthesis hold the inputs that will be passed to the function. Introduction to Matlab Module #3 Page 3

1) Functions • Example Function (1 output) - let’s make a function that will evaluate this expression: y = x 2 + 5 - the user wil pass in the value of x and the function will return y. - let’s call the function “eq_test. m” function [y] = eq_test(x) y = x^2 +5; - this can be called using: >> eq_test(1) or >> c = eq_test(2) Introduction to Matlab Module #3 Page 4

1) Functions • Example Function (2 outputs) - let’s make a function that will calculate the area and circumference of a circle. function [A, C] = circle_comp(r) A = pi*r. ^2; C = 2*pi*r; - when calling this, we need to provide two variables for A and C to be assigned to. >> [Area, Circum] = circle_comp(3) - if two variables are not provided, only the first output will be returned >> circle_comp(2) ans = 12. 5 Introduction to Matlab Module #3 Page 5

2) Files • Importing ASCII Files - You can load in an ASCII file using the command >> load filename. dat - A matrix will be created with the rows and columns found in this file with the name filename - The file is assumed to be space delimited and contain to headers - The Import Wizard allows more complex data files to be read in (i. e. , headers, different delimiters…) • Importing Spreadsheet Files - You can import from excel (*. xls) using the command: >> xlsread(‘filename’) Introduction to Matlab Module #3 Page 6

Lab 3 Exercise Problems - For each of these exercises, you will create a script file. Your script file will perform the calculations and then display the answers to the workspace. - Create a directory on your Z drive called “Z: Matlab_CourseLab 03” - Change your pwd to “Z: Matlab_CourseLab 03” (>> cd Z: Matlab_CourseLab 03) - Perform the following exercises: ASCII Import - download the ASCII file from the course website into your working directory - import the data and plot it. XLS Import - download the XLS spreadsheet file from the course website into your working directory - import the data and plot it. 3. 9 - Create a script file called Lab 03_3 d 9. m to perform the computations 3. 10 - Create a script file called Lab 03_3 d 10. m to perform the computations Introduction to Matlab Module #3 Page 7
- Slides: 7