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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect FXP. 1Winter Quarter Course Wrap Up and Final Review Topics Lecture.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.
Introduction to Matlab
Engineering EG167C - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review.
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 20P. 1Winter Quarter Propulsion Lab with MATLAB Lecture 20.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect FXP. 1Winter Quarter Course Wrap Up and.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
General Computer Science for Engineers CISC 106 Lecture 04 Roger Craig Computer and Information Sciences 9/11/2009.
Introduction to MATLAB
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB.
Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation.
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review Topics.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M2P. 1Winter Quarter Midterm 2 Review Topics.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect FXP. 1Winter Quarter Course Wrap Up and.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 15P. 1Winter Quarter Arrays and Pointers.
Chapter 4 MATLAB Programming Combining Loops and Logic Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
CH Programming An introduction to programming concepts.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
C Programming n General Information on C n Data Types n Arithmetic Operators n Relational Operators n if, if-else, for, while by Kulapan Waranyuwat.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 24P. 1Winter Quarter C++ Lecture 24.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 22P. 1Winter Quarter MATLAB: Simultaneous Equations Lecture 22.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Introduction to MATLAB. CSPP58001 MATLAB MATLAB is is a matrix-based language well suited for carrying out numerical analysis. It has many, many high-
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB Lecture 18.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
1 Lab 1. C Introduction  C: –Developed by Bell lab. in –a procedure-oriented programming language.  Developing environments: –Editing –Preprocessing.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Matrix Algebra Section 7.2. Review of order of matrices 2 rows, 3 columns Order is determined by: (# of rows) x (# of columns)
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 22P. 1Winter Quarter MATLAB: Simultaneous.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
General Computer Science for Engineers CISC 106 Lecture 15 Dr. John Cavazos Computer and Information Sciences 03/16/2009.
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 20P. 1Winter Quarter Propulsion Lab with.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect M2P. 1Winter Quarter Midterm II Review Topics Lecture M2.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 1 Scott Marino.
1 Midterm 1 Review. 2 Midterm 1 on Friday February 27 Closed book, closed notes No computer can be used 50 minutes 4 or 5 questions write full programs.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
Control Statements in Matlab
MATLAB: Structures and File I/O
Matlab review Matlab is a numerical analysis system
Simulation And Modelling
Note on Indexing of Array Elements
Matlab Basics.
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
ME 123 Computer Applications I Lecture 5: Input and Output 3/17/03
Electrical and Computer Engineering Department SUNY – New Paltz
Presentation transcript:

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

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~=

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

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

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

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 = EDU» c= b+a% Add a to each element of b c =

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 = EDU» c = b - a %Subtract a from each element of b c =

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 = EDU» c = a * b % Multiply each element of b by a c =

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 = EDU» c = b / a % Divide each element of b by a c =

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]

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.

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');

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');

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');

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

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);

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)