1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1.

Slides:



Advertisements
Similar presentations
A number of MATLAB statements that allow us to control the order in which statements are executed in a program. There are two broad categories of control.
Advertisements

Strings Testing for equality with strings.
Java Programming Strings Chapter 7.
Programming with MATLAB
Chapter 7. Even in quantitative sciences, we often encounter letters and/or words that must be processed by code You may want to write code that: Reads.
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
CIS 101: Computer Programming and Problem Solving Lecture 7 Usman Roshan Department of Computer Science NJIT.
Additional Data Types: 2-D Arrays, Logical Arrays, Strings Selim Aksoy Bilkent University Department of Computer Engineering
Week 6 - Programming I So far, we’ve looked at simple programming via “scripts” = programs of sequentially evaluated commands Today, extend features to:
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Introduction to Python
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
Additional Data Types: Strings Selim Aksoy Bilkent University Department of Computer Engineering
MATLAB Strings Selim Aksoy Bilkent University Department of Computer Engineering
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.
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.
Creating scalars, vectors, matrices Ex1 & 2. Dot Product & Cross Product Ex3. Plotting Graphs Ex4. Conversion Table Ex5. Plotting functions Finishing Ex4.
Functions 1 parameter, 2 return-values "Conversion of time format" One problem. 5 steps to solve it. 1.
Tutorial 11 Using and Writing Visual Basic for Applications Code
CS1020E Sitin 1 Discussion -- Counting Palindromes.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Mastering Char to ASCII AND DOING MORE RELATED STRING MANIPULATION Why VB.Net ?  The Language resembles Pseudocode - good for teaching and learning fundamentals.
1 Functions 1 Parameter, 1 Return-Value 1. The problem 2. Recall the layout 3. Create the definition 4. "Flow" of data 5. Testing 6. Projects 1 and 2.
1. Reminder of Symbols 2. Dialog Boxes 3. listdlg() 4. msgbox() 5. questdlg() 6. menu() Dialog Boxes Applications of Cell-Arrays 1.
COMP 116: Introduction to Scientific Programming Lecture 28: Midterm #2 Review.
Flow of Control Part 1: Selection
COMP 116: Introduction to Scientific Programming Lecture 24: Strings in MATLAB.
An Introduction to Java Programming and Object-Oriented Application Development Chapter 7 Characters, Strings, and Formatting.
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. Chapter 10: Characters, C- Strings, and More About the string Class.
Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Review while loops Control variables Example Infinite loop
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
Interduction to MATLAB (part 2) Manal Alotaibi Mathematics department College of science King saud university.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
1. Normal arrays of characters 2. Converting a list of strings to a cell- arrays of strings 3. Converting a cell-array of strings to a list of strings.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Strings Characters and Sentences 1.Overview 2.Creating Strings 3.Slicing Strings 4.Searching for substrings 1.
While loops. Iteration We’ve seen many places where repetition is necessary in a problem. We’ve been using the for loop for that purpose For loops are.
Course A201: Introduction to Programming 09/09/2010.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
INTRODUCTION TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in.
Do-While Loops A do-while loop is always guaranteed to iterate at least once. This is because the condition is evaluated at the end of the loop, instead.
Logical Functions Excel Lesson 10.
EEE 161 Applied Electromagnetics
Strings CSCI 112: Programming in C.
Formatting Output.
Dialog Boxes Applications of Cell-Arrays
The Selection Structure
Selection Statements by Ahmet Sacan
Introduction to Scripting
Scripts & Functions Scripts and functions are contained in .m-files
Chapter 7: Strings and Characters
Decision Structures, String Comparison, Nested Structures
String Manipulation Chapter 7 Attaway MATLAB 4E.
Logical Operations In Matlab.
Coding Concepts (Data- Types)
String manipulation string.h library
Chapter 7: Input Validation
Text Manipulation Chapter 7 Attaway MATLAB 5E.
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
C Characters and Strings
Presentation transcript:

1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1

1. Comparing Strings Curious: What would using == do? %hardcode a string. Set in stone. We know what it is. str = ‘Fred’; %test in command window, whether str is equal to ‘Fred’ %(note that it is, so we expect a true!) >> str == 'Fred' 2

1. Comparing Strings Curious: What would using == do? %hardcode a string. Set in stone. We know what it is. str = ‘Fred’; %test in command window, whether str is equal to ‘Fred’ %(note that it is, so we expect a true!) >> str == 'Fred' ans =

1. Comparing Strings Curious: What would using == do? %hardcode a string. Set in stone. We know what it is. str = ‘Fred’; %test in command window, whether str is equal to ‘Fred’ %(note that it is, so we expect a true!) >> str == 'Fred' ans = Matlab compares and evaluates the equality-condition between each letter 1 by 1, to a 0 (for false) or a 1 (for true) 4

1. Comparing Strings, cont. Curious: What would using == do? %hardcode a string. Set in stone. We know what it is. str = ‘Fred’; %test in command window, whether str is equal to ‘Frog’ %(note that it is not, so we expect a false!) >> str == 'Frog' ans =

1. Comparing Strings, cont. Curious: What would using == do? %hardcode a string. Set in stone. We know what it is. str = ‘Fred’; %test in command window, whether str is equal to ‘Frog’ %(note that it is not, so we expect a false!) >> str == 'Frog' ans = letters were identical, 2 letters were not. But.. there is no overall true or false. This is not the way to compare strings. 6

1. Comparing Strings, cont. Curious: What would using == do? %hardcode a string. Set in stone. We know what it is. str = ‘Fred’; %test in command window, is str equal to ‘Flinstones’? %(note that it is not, so we expect a false!) >> str == 'Flintstone’ ??? Error using ==> eq Matrix dimensions must agree. It even gets worse when the length of each string does not match.. It creates an error. Definitely not the right method to compare strings.. 7

1. Comparing Strings, cont. Two built-in functions are commonly used to compare strings: 1. strcmp() – STRing CoMPare returns true if the two arguments are identical strings Practice: strcmp(‘hi’, ‘hi’) evaluates to ______ 8

1. Comparing Strings, cont. Two built-in functions are commonly used to compare strings: 1. strcmp() – STRing CoMPare returns true if the two arguments are identical strings Practice: strcmp(‘hi’, ‘hi’) evaluates to ______ Practice: strcmp(‘HI’, ‘hi’) evaluates to ______ 9

1. Comparing Strings, cont. Two built-in functions are commonly used to compare strings: 1. strcmp() – STRing CoMPare returns true if the two arguments are identical strings Practice: strcmp(‘hi’, ‘hi’) evaluates to ______ Practice: strcmp(‘HI’, ‘hi’) evaluates to ______ Practice: str = ‘yes’; strcmp(str, ‘no’) evaluates to _____ 10

1. Comparing Strings, cont. Two built-in functions are commonly used to compare strings: 1. strcmp() – STRing CoMPare returns true if the two arguments are identical strings Practice: strcmp(‘hi’, ‘hi’) evaluates to ______ Practice: strcmp(‘HI’, ‘hi’) evaluates to ______ Practice: str = ‘yes’; strcmp(str, ‘no’) evaluates to _____ strcmp(‘no’, str) evaluates to _____ 11

1. Comparing Strings, cont. Two built-in functions are commonly used to compare strings: 2. strcmpi() – STRing CoMPare Insensitive returns true if the two arguments are the same string WITHOUT REGARD TO CASE Practice: strcmpi(‘hi’, ‘hi’) evaluates to ______ 12

1. Comparing Strings, cont. Two built-in functions are commonly used to compare strings: 2. strcmpi() – STRing CoMPare Insensitive returns true if the two arguments are the same string WITHOUT REGARD TO CASE Practice: strcmpi(‘hi’, ‘hi’) evaluates to ______ Practice: strcmpi(‘HI’, ‘hi’) evaluates to ______ 13

1. Comparing Strings, cont. Two built-in functions are commonly used to compare strings: 2. strcmpi() – STRing CoMPare Insensitive returns true if the two arguments are the same string WITHOUT REGARD TO CASE Practice: strcmpi(‘hi’, ‘hi’) evaluates to ______ Practice: strcmpi(‘HI’, ‘hi’) evaluates to ______ Practice: str = ‘yes’; strcmpi(str, ‘Yes’) evaluates to _____ 14

1. Comparing Strings, cont. Two built-in functions are commonly used to compare strings: 2. strcmpi() – STRing CoMPare Insensitive returns true if the two arguments are the same string WITHOUT REGARD TO CASE Practice: strcmpi(‘hi’, ‘hi’) evaluates to ______ Practice: strcmpi(‘HI’, ‘hi’) evaluates to ______ Practice: str = ‘yes’; strcmpi(str, ‘Yes’) evaluates to _____ strcmpi(‘YeS’, str) evaluates to _____ 15

Example: Access Granted % ask for username username = input(‘Enter username: ‘, ‘s’); if %correct username % ask for a passwd if %correct password grant access… else quit/end code end else % quit/end code end 16

Example: Access Granted % ask for username username = input(‘Enter username: ‘, ‘s’); if strcmpi(username, ‘John’) % correct username %ask passwd pass = input(‘Enter password: ’, ‘s’); if strcmp(pass, ‘u23!9s2’) %if correct password %grant access... else %quit/end code... end else % quit/end code... end 17 The user name may not be case-sensitive… A password is case-sensitive.

2. Converting strings Convert: string  numbers str2num() str2double() CAUTION: str2double() will convert an entire cell arrays of strings; str2num() will not. Convert: number  string int2str() num2str() 18 How is this used?

Example: prompting inputs Task: Introduce the sensor’s number when prompting. 19

Example: prompting inputs Currently can be done as a combination of an fprintf() and an input() command: %loop to prompt for each value for position = 1:nbSensors fprintf('Enter value of sensor #%d:',position); table(position) = input(' '); %leave a space end 20

Example: prompting inputs Currently can be done as a combination of an fprintf() and an input() command: %loop to prompt for each value for position = 1:nbSensors fprintf('Enter value of sensor #%d:',position); table(position) = input(' '); %leave a space end CAUTION: they cannot be combined. The input() command accepts only 1 string argument, or 2 when prompting for a string (‘s’).  input() never accepts placeholders and variable names. 21

Example: prompting inputs Can be done using string manipulations as well. %loop to prompt for each value for position = 1:nbSensors table(position) = input(['Enter value of sensor #', int2str(position), ': ']); end CAUTION: the [] must be there to concatenate the 3 pieces (first part of the sentence, the number, and the colon) into 1 string. 22

Example: prompting inputs Can be done using string manipulations as well. %loop to prompt for each value for position = 1:nbSensors table(position) = input(['Enter value of sensor #', int2str(position), ': ']); end CAUTION: the [] must be there to concatenate the 3 pieces (first part of the sentence, the number, and the colon) into 1 string. 23 Convert the integer to a string before concatenating all 3 pieces. The digit 1 becomes the string ‘1’, the digit 2 becomes the string ‘2’, etc.. Why:_________

Example: validate input Some students have wondered how to handle this issue: 24 “What happens when the user enters letters (instead of numbers)?” Note: as long as variables d and we do not exist, Matlab loops for us. But “what if”…

Example: validate input, cont. To solve this problem, every input must now be considered as a string, even if they are numbers! Algorithms possible % Grab user’s input as strings % Use str2double() to convert to numbers % Use isnan() to check if the conversion worked % Continue with calculations if it did 25

Example: validate input, cont. To solve this problem, every input must now be considered as a string, even if they are numbers! Algorithms possible % Grab user’s input as strings % Use str2double() to convert to numbers % Use isnan() to check if the conversion worked % Continue with calculations if it did % Grab user’s input as strings % Use str2double() to convert to numbers % while isnan() is true % grab again, convert again % Continue with calculations if it did 26

Example: validate input, cont. What does str2double() and isnan() do? Example when string does look like a number: 27

Example: validate input, cont. What does str2double() and isnan() do? Example when string has an ERROR: 28

Example: validate input, cont. Now, prompt the user for a value, then put the str2double() and isnan() in a loop! 29 EXTREMELY IMPORTANT: isnan() MUST BE THE FIRST CONDITION.

3. Additional String Functions strfind() – find a substring within a string lower() – converts a string to lowercase upper() – converts a string to uppercase isletter() – which characters in the string are letters? 30

Wrapping Up There are many built-in functions for strings 31

Wrapping Up There are many built-in functions for strings Using == to compare strings is really tricky Same length Logical vector returned, not just a 0 or a 1 Use strcmp() to compare 2 strings, case-sensitive Use strcmpi() to compare 2 strings, case-Insensitive 32

Wrapping Up There are many built-in functions for strings Using == to compare strings is really tricky Same length Logical vector returned, not just a 0 or a 1 Use strcmp() to compare 2 strings, case-sensitive Use strcmpi() to compare 2 strings, case-Insensitive There is no mean to compare 3 or 4 strings together. 33

Wrapping Up There are many built-in functions for strings Using == to compare strings is really tricky Same length Logical vector returned, not just a 0 or a 1 Use strcmp() to compare 2 strings, case-sensitive Use strcmpi() to compare 2 strings, case-Insensitive There is no mean to compare 3 or 4 strings together. str2num(), str2double() converts string  numbers num2str(), int2str() converts numbers  strings 34