Lect%2019P.%201 - PowerPoint PPT Presentation

About This Presentation
Title:

Lect%2019P.%201

Description:

A MATLAB script file (Called an M-file) is a text (plain ASCII) file that ... title ('Bike Strain Gage Voltage vs Time') grid %Put a grid on graph. Lect 19 P. 4 ... – PowerPoint PPT presentation

Number of Views:28
Avg rating:3.0/5.0
Slides: 18
Provided by: rick240
Learn more at: http://web.cecs.pdx.edu
Category:
Tags: 2019p | gage | lect

less

Transcript and Presenter's Notes

Title: Lect%2019P.%201


1
MATLAB Script and Function Files
  • Lecture 19

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 (gtgt).

3
MATLAB Script Files
  • This is a MATLAB script file.
  • It has been saved as g13.m".
  • load g13.dat Load data file
  • voltage g13( , 4) Extract volts vector
  • time .0051length(voltage) Create time
    vector
  • plot (time, voltage) Plot volts vs time
  • xlabel ('Time in Seconds') Label x axis
  • ylabel ('Voltage') Label y axis
  • title ('Bike Strain Gage Voltage vs Time')
  • grid Put a grid on graph

4
MATLAB Script Files
  • The preceding file is executed by issuing a
    MATLAB command
  • gtgt g13
  • This single command causes MATLAB to look in the
    current directory, and if a file g13.m is found,
    open it and execute all of the commands. The
    result, in this case, is a plot of the data from
    g13.dat.
  • If MATLAB cannot find the file in the current
    working directory, an error message will appear.

5
MATLAB Script Files
  • When the file is not in the current working
    directory, a cd or chdir command may be issued to
    change the directory.
  • gtgt cd a\ Make a\ the current working
    directory
  • gtgt g13

6
MATLAB Demo
  • MATLAB Example use of ginput
  • clear, clf, hold off
  • axis (0, 10, 0, 10)
  • hold on
  • plot (1, 2, 2, 1, 1,2, 2, 3, 3, 2)
  • text (1, 1.6, 'Click inside box to quit')
  • while 1
  • x, y, buttun ginput (1)
  • if buttun 1, plot (x, y, 'r'), end
  • if buttun 2, plot (x, y, 'oy'), end
  • if buttun 3, plot (x, y, 'g'), end
  • if x gt 1 x lt 2 y gt 2 y lt 3, break end
  • end
  • hold off

7
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.

8
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.

9
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 arg1
    through 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.

10
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.

11
Example of a MATLAB Function File
  • function a , b swap ( a , b )
  • The function swap 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.
  • tempa
  • ab
  • btemp

12
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
  • gtgt x 5 y 6 x , y swap ( x , y )
  • result in
  • x
  • 6
  • y
  • 5

13
MATLAB Function Files
  • Referring to the function, the comments
    immediately following the function definition
    statement are the "help" for the function. The
    MATLAB command
  • gtgthelp swap displays
  • The function swap 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.

14
MATLAB Function Files
  • The MATLAB function must be in the current
    working directory. If it is not, the directory
    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 change
    directories often results in the error message
  • Undefined function or improper matrix assignment.

15
MATLAB Function Files
  • When the function file is not in the current
    working directory, a cd or chdir command may be
    issued to change the directory.
  • gtgt cd a\ Make a\ the current working
    directory

16
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 factorial ( ) 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.

17
MATLAB Function Files
  • function n factorial (k)
  • The function n factorial(k) calculates and
  • returns the value of k factorial. If k is
    negative,
  • an error message is returned.
  • if (k lt 0) n 'Error, negative argument'
  • elseif klt2 n1
  • else
  • n 1
  • for j 2k n n j end
  • end
Write a Comment
User Comments (0)
About PowerShow.com