COMP 116: Introduction to Scientific Programming Lecture 24: Strings in MATLAB.

Slides:



Advertisements
Similar presentations
COMP 116: Introduction to Scientific Programming Lecture 37: Final Review.
Advertisements

C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Computer Science & Engineering 2111 Text Functions 1CSE 2111 Lecture-Text Functions.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
COMP234 Perl Printing Special Quotes File Handling.
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.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
String Functions. Getting Numeric Equivalents of Chars >> double('abc xyz') ans = >> double('ABC XYZ') ans =
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Additional Data Types: 2-D Arrays, Logical Arrays, Strings Selim Aksoy Bilkent University Department of Computer Engineering
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
A.How to create string controls and indicators B.Some string functions C.How to perform file input and output operations D.How to format text files for.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
Additional Data Types: Strings Selim Aksoy Bilkent University Department of Computer Engineering
MATLAB Strings Selim Aksoy Bilkent University Department of Computer Engineering
Floyd, Digital Fundamentals, 10 th ed EET 2259 Unit 13 Strings and File I/O  Read Bishop, Chapter 9.  Lab #13 and Homework #13 due next week.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
AN ENGINEER’S GUIDE TO MATLAB
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (3): MATLAB Environment (Chapter 1)
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
CSE123 Lecture 6 String Arrays. Character Strings In Matlab, text is referred to as character strings. String Construction Character strings in Matlab.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
COMP 116: Introduction to Scientific Programming Lecture 28: Midterm #2 Review.
Introduction to MATLAB Session 3 Simopekka Vänskä, THL Department of Mathematics and Statistics University of Helsinki 2011.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
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)
12/9/2010 Course A201: Introduction to Programming.
ENG College of Engineering Engineering Education Innovation Center 1 Array Accessing and Strings in MATLAB Topics Covered: 1.Array addressing. 2.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
Vladimir Misic: Characters and Strings1Tuesday, 9:39 AM Characters and Strings.
Copyright © Curt Hill Regular Expressions Providing a Search Pattern.
Files: By the end of this class you should be able to: Prepare for EXAM 1. create an ASCII file describe the nature of an ASCII text Use and describe string.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
CSC Programming I Lecture 9 September 11, 2002.
Matlab Basic. MATLAB Product Family 2 3 Entering & Quitting MATLAB To enter MATLAB double click on the MATLAB icon. To Leave MATLAB Simply type quit.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.
1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1.
UniMAP SEM I - 09/10EKT 120 Computer Programming1 Lecture 8 – Arrays (2) & Strings.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Strings Characters and Sentences 1.Overview 2.Creating Strings 3.Slicing Strings 4.Searching for substrings 1.
Cell Arrays: An Introduction Without an understanding of how cell arrays work and how to interact with them, cell arrays can be one of the most frustrating.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Characters and Text You can assign text strings to MATLAB variables using single quotes. For example, c = 'Hello'; The result is not a numeric matrix or.
C Characters and Strings
String Processing Upsorn Praphamontripong CS 1110
Introduction to Programming for Mechanical Engineers (ME 319)
Strings Part 1 Taken from notes by Dr. Neil Moore
Chapter 8 - Characters and Strings
Introduction to C++ Programming
String Manipulation Chapter 7 Attaway MATLAB 4E.
Introduction to Computer Science
C++ Programming Lecture 20 Strings
Text Manipulation Chapter 7 Attaway MATLAB 5E.
Python Strings.
C Characters and Strings
Strings Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Presentation transcript:

COMP 116: Introduction to Scientific Programming Lecture 24: Strings in MATLAB

So far…. Vectors, matrices ◦ Of numbers Linear systems if-elseif-else-end Loops: while, for functions Today: letters, words, sentences ◦ characters and strings

Characters and strings Characters: ‘a’, ‘b’, ‘3’, ‘ ’ ◦ Letters, digits, punctuation, symbols, whitespace(space, newline, … ) Strings are sequences(vectors) of characters ◦‘Meep’ is nothing but [‘M’, ‘e’, ‘e’, ‘p’] ◦[‘Bat’, ‘man’] gives ‘Batman’ ◦NOTE: Strings are rows (not columns) ‘’ % empty string, length == 0 ‘a’ % Single character ‘COMP 116’

Why do we need strings ? Text Input / Output ◦ Remember >>input(‘Give me a number:’)? File Input / Output Analyzing text

Constructing Strings Enclose string in single quotes ◦'This is a string' ◦ NOTE: the actual quote symbols are apostrophes Row vector of characters ◦['a', 'n', 'o', 't', 'h', 'e', 'r' ]

Strings as a vector of chars Can be manipulated like any other vector s1 = 'The quick brown fox ' s2 = 'jumped over the lazy dog' s = [s1 s2] % concatenate strings s(5) % ans = q s(17:19) % ans = fox jIdx = find( s == 'j' ) % ans=21 jStr = s(jIdx:jIdx+3) % ans = jump

Exercise Assume that str=‘this is a string’. What do the following commands do: >>str(1:4) >>str(1)==‘t’ >>str(1)=‘T’ >>str(end:-1:1)

String operations Combining Case Conversion (upper/lower) Conversion Comparison Search / Replace Other useful functions

#1: Combining strings (concatenation) Given a, b - both string variables Same as concatenating arrays ◦[ a b ] a = ‘With great power'; b = ‘comes great responsibility'; space = ' '; s = [ a space b ]

#2: Case conversion lower(varString) ◦ Converts to lower case upper(varString) ◦ Converts to upper case lower( ‘COMP 116' ) % ans = ‘comp 116' upper(‘I am not shouting!’)

Exercise Write a function that given a string str returns a string upper_str that is the same as str except the first character of every word in upper_str is uppercase i.e. given ‘this is a string’ the function should return ‘This Is A String’ HINT: A word starts at the first position (of the string) or after a space character You will need for loops, if-else,==,upper

#3: Comparison Remember: strings are vectors of chars s1 = 'abcd'; s2 = 'xyzw'; s3 = 'cat'; Avoid normal comparison operators! ◦s1 == s2, s1 = s3 ◦ Operators work element by element (on characters) ◦ Thus, strings (i.e., the vector of chars) must be same length Use string comparison functions instead ◦strcmp(a,b), true when a and b are the same ◦strcmpi, string comparison while ignoring case ◦strncmp, strncmpi :  Similar, but compares first n characters only

#4: Conversion: strings and numbers num2str() ◦ Converts numbers to strings ◦ Handles real or complex number str2num() ◦ Converts strings to numbers ◦ Handles real or complex number strings num2str( i ) % ans = ‘ i’ disp([‘You took ’ num2str(num_guesses) ‘ tries’]); str2num( ‘ i’ ) % ans = i

#5: Searching strfind ◦ Search for a string inside another string ◦ returns indices to start of each instance strVal = ['with great power comes great responsibility.']; strfind( strVal, 'great') % ans = [6 24]

Exercise Write a function that given a string str, returns a string replaced_str which is the same as str except all occurrences of ‘great’ in str have been replaced by ‘little’ Write a function that given strings str,a,b replaces all occurrences of a in str by b. ◦ Modify the function so that it also returns the number of replacements.

Other string functions Look up what these functions do ◦strcat ◦strrep ◦strtok ◦mat2str ◦str2angle ◦strtrim ◦deblank ◦strjust ◦regexp ◦strread ◦textread ◦textscan ◦…

Review Strings doc strings From MATLAB Help window, Contents tab, choose MATLAB → Programming Fundamentals → Characters and Strings.