MATLAB: Structures, File I/O - Operators, Scalar-Matrix Ops, File I/O, Slides of Computer Engineering and Programming

An overview of various matlab operators and structures, including relational and logical operators, selection structures (if-elseif-else), scalar-matrix addition, subtraction, multiplication, and division, and file i/o using load, fopen, fgets, fscanf, and fprintf functions.

Typology: Slides

2012/2013

Uploaded on 05/06/2013

apsara
apsara 🇮🇳

4.5

(2)

86 documents

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MATLAB: Structures and File I/O
Lecture 21
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download MATLAB: Structures, File I/O - Operators, Scalar-Matrix Ops, File I/O and more Slides Computer Engineering and Programming in PDF only on Docsity!

MATLAB: Structures and File I/O

Lecture 21

MATLAB Relational Operators

  • MATLAB supports six relational operators.

Less Than < Less Than or Equal <= Greater Than > Greater Than or Equal >= Equal To == Not Equal To ~=

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

MATLAB Repetition Structures

  • A for loop in MATLAB for x = array for x=1: 0.5 : 10 % execute these commands end
  • A while loop in MATLAB while expression while x <= 10 % execute these commands end

Scalar - Matrix Subtraction

EDU» a=3; EDU» b=[1, 2, 3;4, 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

Scalar - Matrix Multiplication

EDU» a=3; EDU» b=[1, 2, 3;4, 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

The. operator

  • If you want to do element by element operation on a vector or matrix.
  • Consider
    • A = [2, 4, 5, 8]
    • B = [1, 2, 2, 4]
  • Then
    • C = A./B is [2,2,2.5,2]
  • And
    • D = 1./A is [.5,.25,.2,.125]

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 Docsity.com,

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,... '%s%s%*s%f'); Docsity.com

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); Docsity.com

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,'%f%f\n',a,b);

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)