1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (3): MATLAB Environment (Chapter 1)

Slides:



Advertisements
Similar presentations
The Web Warrior Guide to Web Design Technologies
Advertisements

Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction 9.2Streams 9.3Formatting Output with printf 9.4Printing Integers 9.5Printing Floating-Point.
1 Outline 13.1Introduction 13.2A Simple Program: Printing a Line of Text in a Web Page 13.3Another JavaScript Program: Adding Integers 13.4Memory Concepts.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
JavaScript, Third Edition
String Escape Sequences
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files called M- files. M-files are.
AN ENGINEER’S GUIDE TO MATLAB
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.
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 9 - Formatted Input/Output Outline 9.1Introduction.
Chapter 9 Formatted Input/Output. Objectives In this chapter, you will learn: –To understand input and output streams. –To be able to use all print formatting.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 4.
Chapter 9 Formatted Input/Output Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 Pearson Education, Inc. All rights reserved Formatted Output.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Input, Output, and Processing
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Linux Operations and Administration
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Asking the USER for values to use in a software 1 Input.
ENG College of Engineering Engineering Education Innovation Center 1 More Script Files in MATLAB Script File I/O : Chapter 4 1.Global Variables.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
 2000 Deitel & Associates, Inc. All rights reserved. Outline 8.1Introduction 8.2A Simple Program: Printing a Line of Text in a Web Page 8.3Another JavaScript.
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.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Asking the USER for values to use in a software 1 Input.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
Lecture 6: Output 1.Presenting results in a professional manner 2.semicolon, disp(), fprintf() 3.Placeholders 4.Special characters 5.Format-modifiers 1.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
28 Formatted Output.
C Formatted Input/Output
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
ICS103 Programming in C Lecture 3: Introduction to C (2)
Introduction to Scripting
Other Kinds of Arrays Chapter 11
Intro to PHP & Variables
INPUT & OUTPUT scanf & printf.
String Manipulation Chapter 7 Attaway MATLAB 4E.
Introduction to Java Applications
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Presentation transcript:

1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (3): MATLAB Environment (Chapter 1)

2 Objectives  CHAPTER (1):MATLAB Environment-cont -Use strings and perform simple string operations. -Perform some basic input/output (I/O) operations using fprintf and input statements. -Creating a script M-file.

3 1.9 Strings  Strings are matrices with character elements.  String constants are enclosed in single quotes » x='abcd' x = abed

4 1.9 Strings  How do we create a character string that already contains the single-quote character? As an example, let's specify the string didn't? Two single quotes in a row (' ') give a different result than a double quote (").

5 1.9 Strings  Escape Characters This table lists the escape character sequences you use to specify nonprinting characters in a format specification. Character Description \b Backspace \f Form feed \n New line \r Carriage return \t Horizontal tab \\ Backslash \ '' or ''(two single quotes)Single quotation mark % Percent character

6 1.9 Strings  String Indexing The first character of a string has an index of 1. To access any character, we specify the variable name with the index of the character enclosed in parentheses. » a = ‘abed’ a= abed To look at the first character in the string, use index 1. » a(l) ans = a » a(3) ans = e

7 1.9 Strings  Concatenating Strings Strings can be concatenated (that is, linked to make a larger string) by using the following notation: Let's create three strings x1,and x2 » x1='abcd' x1 = abed » x2='efgh' x2 = efgh We will now create a new variable that contains the concatenation of x1 and x2: » x= [x1, x2] x = abedefgh

8 1.9 Strings  Concatenating Strings » mm=['xyzzy',xl,'123',x2] mm = xyzzyabed123efgh » a=[xl(l), x2(1)] a = ae » b=[xl(1:2), x2(1:3)] b = abefg

9 1.9 Strings  Concatenating Strings

Strings  String Functions

Strings  String Functions STRREP Replaces characters in a string with different characters. UPPER Converts a string to uppercase. LOWER Converts a string to lowercase. Examples: » a='abedefgh' a = abedefgh » length(a) ans = 8

Strings  String Functions num2str converts a number to a string >> msg1 = [’There are ’,num2str(100/2.54),’ inches in a meter’] msg1 = There are inches in a meter For greater control over format of the number-to-string conversion, use The str2num function converts a string representation of a number to its corresponding numeric value. For example, if we add' 123' to '321', we get a strange result: » '123' + '321' ans =

Strings  String Functions num2str converts a number to a string >> msg1 = [’There are ’,num2str(100/2.54),’ inches in a meter’] msg1 = There are inches in a meter For greater control over format of the number-to-string conversion, use The str2num function converts a string representation of a number to its corresponding numeric value. For example, if we add' 123' to '321', we get a strange result: » '123' + '321' ans = This result is actually the sum of the ASCII codes for the two numbers. The first 100 is the ASCII code for 1 plus the ASCII code for 3, …..etc

Strings  String Functions The ABS function gets the ASCII codes for a string: » abs('123') ans = If we want to interpret a string as a number, we use the STR2NUM function to convert the string to a number: » str2num('123') + str2num('321') ans = 444 Function STR2NUM converts the character string '123' to the number 123. » num2str('123') ans = 123

Strings  String Functions The char function can be used to combine strings >> both = char(msg1,msg2) both = There are inches in a meter There are cubic inches in a liter or to refer to individual characters by their ASCII codes1 >> char(49) ans = 1 >> char([ ]) ans = MATLAB

Strings  String Functions Use strcmp to test whether two strings are equal, i.e., if they contain the same sequence of characters. >> msg1 = [’There are ’,num2str(100/2.54),’ inches in a meter’]; >> msg2 =’There are cubic inches in a liter’); >> strcmp(msg1,msg2) ans = 0 Compare the first n characters of two strings with strncmp >> strncmp(msg1,msg2,9) ans = 1 The first nine characters of both strings are “There are”, so strncmp(msg1,msg2,9) returns 1, or true.

Strings  String Functions Locate occurances of one string in another string with findstr >> msg1 = [’There are ’,num2str(100/2.54),’ inches in a meter’] msg1 = There are inches in a meter >> findstr(’in’,msg1) ans = >> msg1(19:20) ans = in

Strings  String Functions  The last string function we will look at is STRREP. This function replaces characters in a string with different characters. » xx='String search and replacement'; » yy=strrep( xx,'a', '*') yy= String se*rch *nd repl*cement  We can also replace strings of characters with new strings: » line='supplement replacement excitement retirement'; » yy=strrep(line, 'ent', '***') yy = supplem*** replacem*** excitem*** retirem***

Input and Output Statements  Every time we make a calculation or assign a variable, MATLAB displays the result automatically.  Everything we have entered has been a one-line calculation. We type in the equation and MATLAB gives us the result.  Most programs, however, ask for input, perform a large number of calculations, and then at the end, display the final results. The intermediate results are not displayed.  This raises the questions: How do we suppress the results of intermediate equations, how does a program request input from the user, and how does a program display results?

Input and Output Statements The Semicolon in MATLAB Results of intermediate steps can be suppressed with semicolons. Example: Assign values to x, y, and z, but only display the value of z in the command window: >> x = 5; >> y = sqrt(59); >> z = log(y) + x^0.25 z =

Input and Output Statements The Semicolon in MATLAB Multiple Statements per Line Use commas or semicolons to enter more than one statement at once. Commas allow multiple statements per line without suppressing output. >> a = 5; b = sin(a), c = cosh(a) b = c =

Input and Output Statements Text Input and Output It is usually desirable to print results to the screen or to a file. On rare occasions it may be helpful to prompt the user for information not already provided by the input parameters to a function. Inputs to functions: input function can be used Input parameters to functions are preferred. Text output from functions: disp function for simple output fprintf function for formatted output.

Input and Output Statements MATLAB Programmed Output Output to the command window is achieved with either the disp function or the fprintf function. Output to a file requires the fprintf function.  disp Simple to use. Provides limited control over appearance of output.  fprintf Slightly more complicated than disp. Provides total control over appearance of output.

Input and Output Statements MATLAB Programmed Output The fprintf function Syntax: fprintf(outFormat,outVariables) fprintf(fileHandle,outFormat,outVariables) In the first form (no fileHandle) the output is displayed in the command window. In the second form, the output is written to a file referred to by the fileHandle (more on this later). Example: » fprintf('This is a test.\n'); This is a test.

Input and Output Statements MATLAB Programmed Output Example:

Input and Output Statements MATLAB Programmed Output- Example: » a=3*pi; » b=pi/3; » fprintf('The value of a is %g, the value of b is %g, and that is that.\n', a, b);. The value of a is , the value of b is , and that is that. Another Example: What is the output?!!

Input and Output Statements The output is:

Input and Output Statements we can use a string variable to replace the character string: The two methods produce the same result. The second method can also be used to display the values of variables:

Input and Output Statements The outFormat string can contain anytext characters. It also must contain a conversion code for each of the outVariables. The following table shows the basic conversion codes.

Input and Output Statements In addition to specifying the type of conversion (e.g. %d, %f, %e) one can also specify the width and precision of the result of the conversion. Syntax: %wd %w.pf %w.pe where w is the number of characters in the width of the final result, and p is the number of digits to the right of the decimal point to be displayed.

Input and Output Statements Conversion specifications begin with the % character and contain these optional and required elements:  Flags (optional)  Width and precision fields (optional)  A subtype specifier (optional)  Conversion character (required) You specify these elements in the following order: % e Conversion character Flag Field width precision

Input and Output Statements Flags: You can control the alignment of the output using any of these optional flags (-, +,space, 0). Field Width and Precision Specifications: You can control the width and precision of the output by including these options in the format string. Conversion Characters: Conversion characters specify the notation of the output.

Input and Output Statements

Input and Output Statements

Input and Output Statements The disp function Syntax: disp(outMatrix) where outMatrix is either a string matrix or a numeric matrix. Examples: Numeric output Examples: String output

Input and Output Statements MATLAB Programmed In put. The input function can be used to prompt the user for numeric or string input. With this function, MATLAB displays the text string and then requests input from you in the form of a number. The value you enter is placed in the variable,

Input and Output Statements MATLAB Programmed In put-Examples. Example1 The form of the INPUT function shown previously requires the input of a numerical value. If we type in an invalid response, MATLAB generates an error message and then repeats the message:

Input and Output Statements Example2

Input and Output Statements Example3 A second form of the INPUT function allows you to input a character string:

MATLAB Script Files  A script file is a text file that contains a sequence of MATLAB commands.  Using a script file is equivalent to typing in a series of commands at the MATLAB command prompt, except that you don't need to type the commands each time you wish to use them.  Programs are contained in m-files Plain text files – not binary files produced by word processors File must have “.m” extension  m-file must be in the path

MATLAB Script Files  Basis for programming  M-files can be: Files that store sequences of commands that can be used over and over User Defined Functions  Go to New to m-file An Editor will open up  Matlab is a scripting language You don’t have to worry about all the standard language overhead!

MATLAB Script Files

43