1 / 21

Matlab : Script and Function Files

Matlab : Script and Function Files. Lecture 2. M ATLAB Script Files. A MATLAB script file (Called an M-file) is a text (plain ASCII) file that contains one or more MATLAB commands and, optionally, comments. The file is saved with the extension ". m ".

gina
Download Presentation

Matlab : Script and Function Files

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Matlab: Script and Function Files Lecture 2

  2. MATLAB Script Files • A MATLAB script file (Called an M-file) is a text (plain ASCII) file that contains one or more MATLAB commands and, optionally, comments. • The file is saved with the extension ".m". • When the filename (without the extension) is issued as a command in MATLAB, the file is opened, read, and the commands are executed as if input from the keyboard. The result is similar to running a program in C. The following slide is the contents of an M-file. Note that there are no prompts (>>).

  3. Matlab Script Files • EXAMPLE at/Users/keady/Documents/Matlab/MatlabChapman/book/matlab/4e/chap1/calc_area.m

  4. Matlab Script Files • The classes Tue, Wed, Thu are to cover Chapman’s Chap 2,3,4,5,6 • Chapman begins using script files from Chap2 • He is later at bringing in user-defined functions than I favour. See Chap 5.

  5. MATLAB Script Files • The preceding file is executed by issuing a MATLAB command: >>calc_area • This single command causes MATLAB to look in the path, and if a file calc_area.m is found, open it and execute all of the commands. • If MATLAB cannot find the file in the path, an error message will appear.

  6. Matlab Script Files • Script files can be edited more easily than just heaps of lines in the command window.E.g. modification calc_areaGK1 uses the inputfunction to take input of the radius at the command line.

  7. Matlab Script Files • Chapman S2.12temp_conversionis another example of use ofinputAlso has a more advanced outputfprintfformatted print

  8. MATLAB Script Files • For small tasks, changing the path is a bit tedious. Fixes (i) Keep the files in a directory from which matlab is launched. pwd tells you the working directory. (ii) The cd command may be issued to change the directory. >> cd('somewhere ') % Make somewhere the current working directory >>

  9. MATLAB Demo • ginputEx is another, more elaborate, script file • Really belongs later, as Chapman Chpt 9 has more on graphics. • I put it here as last time a student asked ‘how do I get coordinates off a map?’ and the ginputEx gives an indication that it can be done.

  10. MATLAB Function Files • A MATLAB function file (called an M-file) is a text (plain ASCII) file that contains a MATLAB function and, optionally, comments. • The file is saved with the function name and the usual MATLAB script file extension, ".m". • A MATLAB function may be called from the command line or from any other M-file.

  11. MATLAB Function Files • See calc_areaFunfor a first example

  12. MATLAB Function Files • When the function is called in MATLAB, the file is accessed, the function is executed, and control is returned to the MATLAB workspace. • Since the function is not part of the MATLAB workspace, its variables and their values are not known after control is returned. • Any values to be returned must be specified in the function syntax.

  13. MATLAB Function Files • The syntax for a MATLAB function definition is: function [val1, … , valn] = myfunc (arg1, … , argk) where val1 through valn are the specified returned values from the function and arg1through argk are the values sent to the function. • Since variables are local in MATLAB (as they are in C), the function has its own memory locations for all of the variables and only the values (not their addresses) are passed between the MATLAB workspace and the function.

  14. MATLAB Function Files • It is OK to use the same variable names in the returned value list as in the argument. The effect is to assign new values to those variables. As an example, the following slide shows a function that swaps two values.

  15. Example of a MATLAB Function File function [ a , b ] = swapGK( a , b ) % The function swapGK receives two values, swaps them, % and returns the result. The syntax for the call is % [a, b] = swap (a, b) where the a and b in the ( ) are the % values sent to the function and the a and b in the [ ] are % returned values which are assigned to corresponding % variables in your program. temp=a; a=b; b=temp;

  16. Example of a MATLAB Function File • To use the function a MATLAB program could assign values to two variables (the names do not have to be a and b) and then call the function to swap them. For instance the MATLAB commands: >> x = 5 ; y = 6 ; [ x , y ] = swap ( x , y ) result in: x = 6 y = 5

  17. MATLAB Function Files • Referring to the function, the comments immediately following the function definition statement are the "help" for the function. The MATLAB command: >>help swapGK%displays: The function swapGK receives two values, swaps them, and returns the result. The syntax for the call is [a, b] = swapGK (a, b) where the a and b in the ( ) are the values sent to the function and the a and b in the [ ] arereturned values which are assigned to corresponding variables in your program.

  18. MATLAB Function Files • Usual caution. The MATLAB function must be in the path. If it is not, this must be changed before calling the function. • If MATLAB cannot find the function or its syntax does not match the function call, an error message will appear. Failure to get the path correct often results in the error message: Undefined function or improper matrix assignment.

  19. MATLAB Function Files • Unlike C, a MATLAB variable does not have to be declared before being used, and its data type can be changed by assigning a new value to it. • For example, the function factorialGK ( ) on the next slide returns an integer when a positive value is sent to it as an argument, but returns a character string if the argument is negative

  20. MATLAB Function Files function [n] = factorialGK(k) % The function [n] = factorialGK(k) calculates and % returns the value of k factorial. If k is negative, % an error message is returned. if (k < 0) n = 'Error, negative argument'; elseifk<2 n=1; else n = 1; for j = [2:k] n = n * j; end end

More Related