Lect 21P. 1 - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Lect 21P. 1

Description:

MATLAB: Structures and File I/O Lecture 21 – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 17
Provided by: RickF160
Category:
Tags: 21p | lect | looping | structures

less

Transcript and Presenter's Notes

Title: Lect 21P. 1


1
MATLAB Structures and File I/O
  • Lecture 21

2
MATLAB Relational Operators
  • MATLAB supports six relational operators.
  • Less Than lt
  • Less Than or Equal lt
  • Greater Than gt
  • Greater Than or Equal gt
  • Equal To
  • Not Equal To

3
MATLAB Logical Operators
  • MATLAB supports three logical operators.
  • not highest precedence
  • and next highest precedence
  • or next highest precedence

4
MATLAB Selection Structures
  • An if - elseif - else structure in MATLAB.
  • Note that elseif is one word.
  • if expression1 is true
  • execute these commands
  • elseif expression2 is true
  • execute these commands
  • else the default
  • execute these commands
  • end

5
MATLAB Repetition Structures
  • A for loop in MATLAB for x array
  • for x1 0.5 10
  • execute these commands
  • end
  • A while loop in MATLAB while expression
  • while x lt 10
  • execute these commands
  • end

6
Scalar - Matrix Addition
  • EDU a3
  • EDU b1, 2, 34, 5, 6
  • b
  • 1 2 3
  • 4 5 6
  • EDU c ba Add a to each element of b
  • c
  • 4 5 6
  • 7 8 9

7
Scalar - Matrix Subtraction
  • EDU a3
  • EDU b1, 2, 34, 5, 6
  • b
  • 1 2 3
  • 4 5 6
  • EDU c b - a Subtract a from each element of b
  • c
  • -2 -1 0
  • 1 2 3

8
Scalar - Matrix Multiplication
  • EDU a3
  • EDU b1, 2, 34, 5, 6
  • b
  • 1 2 3
  • 4 5 6
  • EDU c a b Multiply each element of b by a
  • c
  • 3 6 9
  • 12 15 18

9
Scalar - Matrix Division
  • EDU a3
  • EDU b1, 2, 34, 5, 6
  • b
  • 1 2 3
  • 4 5 6
  • EDU c b / a Divide each element of b by
    a
  • c
  • 0.3333 0.6667 1.0000
  • 1.3333 1.6667 2.0000

10
Reading Data from Files
  • The load command does not always succeed in
    reading an input file. It only works when all
    lines in the file have the same ASCII format.
    Files with header material do not qualify, nor do
    binary files.
  • ASCII files that cannot be input with the load
    function can be opened and input with MATLAB
    functions that are similar to C language
    functions we have been using. The MATLAB
    functions include fopen, fgets, fscanf, and
    sscanf.

11
Opening Data files
  • The command to open a file for input is
  • infile_id fopen('filename','r')
  • The command to open a file for output is
  • outfile_id fopen('filename','w')

12
Inputing Data from Open Files
  • After the file is opened for reading we can input
    one line at a time as a text string. The command
    to read one line from the file as a string is
  • line fgets (infile_id)
  • If the remainder of the data in the file all has
    the same format we can read the rest of the file
    and store it in an array with the command
  • myarray fscanf(infile_id,...
  • 'sssf')

13
Inputting Data from Open Files
  • Data that has been read in as a string using
    fgets can be parsed using the function sscanf.
  • The syntax to input a line and then extract and
    discard three columns from the string and save
    the fourth column of floating point data in an
    element in the array myarray would be
  • line fgets(infile_id)
  • myarray(k)sscanf(line,'sssf')

14
Note on Indexing of Array Elements
  • Indexing in C/C
  • float myarray20
  • int i
  • for (i 0 i lt 20 i)
  • myarray i 0.0
  • Indexing in MATLAB
  • No declarations
  • are necessary
  • for i 120
  • myarray( i ) 0.0
  • end

15
Printing Data to Files
  • When a file has been opened for output, the
    command to output one line to the file as a
    string is
  • fprintf(outfile_id,'s',line)
  • Lines of other data can be output by inserting
    the appropriate format string and variable names.
    For example
  • fprintf(outfile_id,'ff\n',a,b)

16
Printing to the Screen
  • Data can also be printed to the screen with the
    fprintf function. To do so, we omit the file
    idenification (outfile_id).
  • For instance, to print a line of text to the
    screen we could use
  • fprintf('s',line)
  • and to print other data we might use
  • fprintf('f f\n', a , b)
Write a Comment
User Comments (0)
About PowerShow.com