Engineering H192 - Computer Programming MATLAB: Structures and File I/O Lecture 21 Winter Quarter Gateway Engineering Education Coalition Lect 21 P. 1 Engineering H192 - Computer Programming 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 Winter Quarter < <= > >= == ~= Gateway Engineering Education Coalition Lect 21 P. 2 Engineering H192 - Computer Programming MATLAB Logical Operators • MATLAB supports three logical operators. not and or Winter Quarter ~ & | % highest precedence % next highest precedence % next highest precedence Gateway Engineering Education Coalition Lect 21 P. 3 Engineering H192 - Computer Programming 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 Winter Quarter Gateway Engineering Education Coalition Lect 21 P. 4 Engineering H192 - Computer Programming 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 Winter Quarter Gateway Engineering Education Coalition Lect 21 P. 5 Engineering H192 - Computer Programming Scalar - Matrix Addition EDU» a=3; EDU» b=[1, 2, 3;4, 5, 6] b= 1 2 3 4 5 6 EDU» c= b+a % Add a to each element of b c= 4 5 6 7 8 9 Winter Quarter Gateway Engineering Education Coalition Lect 21 P. 6 Engineering H192 - Computer Programming 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 Winter Quarter Gateway Engineering Education Coalition Lect 21 P. 7 Engineering H192 - Computer Programming 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 Winter Quarter Gateway Engineering Education Coalition Lect 21 P. 8 Engineering H192 - Computer Programming Scalar - Matrix Division EDU» a=3; EDU» b=[1, 2, 3;4, 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 Winter Quarter Gateway Engineering Education Coalition Lect 21 P. 9 Engineering H192 - Computer Programming 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. Winter Quarter Gateway Engineering Education Coalition Lect 21 P. 10 Engineering H192 - Computer Programming 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'); Winter Quarter Gateway Engineering Education Coalition Lect 21 P. 11 Engineering H192 - Computer Programming 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'); Winter Quarter Gateway Engineering Education Coalition Lect 21 P. 12 Engineering H192 - Computer Programming 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,'%*s%*s%*s%f'); Winter Quarter Gateway Engineering Education Coalition Lect 21 P. 13 Engineering H192 - Computer Programming Note on Indexing of Array Elements • Indexing in C/C++ • Indexing in MATLAB float myarray[20]; int i; % No declarations % are necessary for (i = 0; i < 20; i++) { myarray[ i ] = 0.0; } for i = [1:20] Winter Quarter myarray( i ) = 0.0; end Gateway Engineering Education Coalition Lect 21 P. 14 Engineering H192 - Computer Programming 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); Winter Quarter Gateway Engineering Education Coalition Lect 21 P. 15 Engineering H192 - Computer Programming 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) Winter Quarter Gateway Engineering Education Coalition Lect 21 P. 16