19
String Manipulat ion Chapter 7

String Manipulation

  • Upload
    louisa

  • View
    53

  • Download
    0

Embed Size (px)

DESCRIPTION

String Manipulation. Chapter 7. String Processing. 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 data files that contain both numbers and words Writes data to files (filenames are strings) - PowerPoint PPT Presentation

Citation preview

Page 1: String Manipulation

String Manipulation Chapter 7

Page 2: String Manipulation

String ProcessingEven in quantitative sciences, we often encounter letters and/or words that must be processed by code

You may want to write code that:• Reads data files that contain both

numbers and words• Writes data to files (filenames are strings)• Read a list of files• Combine a string and a number

• Label plot titles or axes with nums and words• Prints messages with words and numbers

mixed

Page 3: String Manipulation

Common Earth Science Data SetsAdvanced National Seismic System (ANSS)

• http://www.ncedc.org/anss/ • Near real-time data for all global

earthquake events!• Not just numbers

USGS Current Water Data • http://waterdata.usgs.gov/nwis/rt • Near real-time data for streams in

all 50 states!• Not just numbers

Page 4: String Manipulation

Strings: A Quick Review• Recall that

strings are arrays of characters

• i.e. a matrix of characters

Page 5: String Manipulation

Strings: A Quick Review• Strings can form

rectangular matrices too!

• Must follow same matrix rules

• Rows and Cols must be consistent

Page 6: String Manipulation

Strings: A Quick Review• Strings can form

rectangular matrices too!

• Must follow same matrix rules

• Rows and Cols must be consistent

• Can “pad” each row with blank spaces to make columns consistent

• You can read about “strcat”, “srtvcat”, and “char”

Page 7: String Manipulation

Formatted Strings: sprintfWhat if you want to combine a numeric result with a word?

• fprintf: only prints to the command window (can’t store/use the result)• sprintf: works just like fprintf, but makes a string

Page 8: String Manipulation

Custom Plot Labels Using sprintfWhat if you want to label a plot with numbers from data?

• Most MATLAB commands for labeling only accept strings• Use sprintf to make a string with text+number. Then use the string

Page 9: String Manipulation

Custom Plot Labels Using sprintfWhat if you want to label a plot with numbers from data?

• Most MATLAB commands for labeling only accept strings• Use sprintf to make a string with text+number. Then use the string

Page 10: String Manipulation

Other String Manipulation Functions• Attaway also covers

• deblank• char• strtrim• upper• lower

• These may come in handy someday• You can read about these functions for their usage

Page 11: String Manipulation

Comparing StringsWhat if you want to see if two strings are identical?• Do not use “==“

• Compares ASCII values• May work, but is bad

programming style, and can produce unwanted results

• Use “strcmp” or “strncmp”• Compares strings

Page 12: String Manipulation

Comparing StringsWhat if you want to see if two strings are identical?strcmp• compares two strings

• returns logical true if identical

strncmp• compares the first n characters of

two strings• Returns logical true if identical

• You can read about “strfind”, “findstr”, “strrep”, and “strtok”.

Page 13: String Manipulation

“is*” FunctionsNow that you know about various variable types in MATLAB…• May want to test whether

a variable contains a certain type of data

• Various “is” functions do this

• ischar• isletter• isspace

http://www.mathworks.com/help/matlab/ref/is.html

Page 14: String Manipulation

Converting Numbers to Stringsint2str

• Converts an int to a string• Automatically rounds off if needed

num2str• Converts an number (double or int)

to a string• Can specify precision• If precision not specified, uses

current MATLAB defaults

Page 15: String Manipulation

Converting Strings to Numbers

str2num• Converts a string to a number

(double)

Page 16: String Manipulation

Practical Example:Reading Data FilesWhat if you have a bunch of files that you want to load?• Don’t load each one, one

by one by hand• Waste of time!

• Knowledge of string processing can help!

• Use “ls” as a function• Store result as character

array

Page 17: String Manipulation

Plotting Data From Multiple Files

Page 18: String Manipulation

Plotting Data From Multiple Files

Fair Warning:• Data files are read in the

order that ls reports.• If not in order, you will

have to sort the data.

Page 19: String Manipulation

Final Thoughts• Even though the focus of most scientific tasks is to

crunch numbers…• Knowing how to deal with strings is beneficial

What’s Next?•What if our data files have a mix of numbers

and letters/words?•Need a new class of variable:

cell arrays and structures!