Presentation is loading. Please wait.

Presentation is loading. Please wait.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.

Similar presentations


Presentation on theme: "Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures."— Presentation transcript:

1 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures and File I/O Lecture 21

2 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 2Winter Quarter 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~=

3 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 3Winter Quarter MATLAB Logical Operators MATLAB supports three logical operators. not~% highest precedence and&% next highest precedence or |% next highest precedence

4 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 4Winter Quarter MATLAB Selection Structures An if - elseif - else structure in MATLAB. Note that elseif is one word. ifexpression1% is true % execute these commands elseif expression2% is true % execute these commands else% the default % execute these commands end

5 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 5Winter Quarter 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 MATLABwhile expression while x <= 10 % execute these commands end

6 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 6Winter Quarter 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

7 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 7Winter Quarter 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

8 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 8Winter Quarter 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

9 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 9Winter Quarter 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

10 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 10Winter Quarter 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]

11 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 11Winter Quarter 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.

12 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 12Winter Quarter 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');

13 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 13Winter Quarter 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');

14 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 14Winter Quarter 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');

15 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 15Winter Quarter Note on Indexing of Array Elements Indexing in C/C++ float myarray[20]; int i; for (i = 0; i < 20; i++) { myarray[ i ] = 0.0; } Indexing in MATLAB % No declarations % are necessary for i = [1:20] myarray( i ) = 0.0; end

16 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 16Winter Quarter 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);

17 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 17Winter Quarter 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)


Download ppt "Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures."

Similar presentations


Ads by Google