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
String Manipulation Chapter 7
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
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
Strings: A Quick Review• Recall that
strings are arrays of characters
• i.e. a matrix of characters
Strings: A Quick Review• Strings can form
rectangular matrices too!
• Must follow same matrix rules
• Rows and Cols must be consistent
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”
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
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
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
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
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
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”.
“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
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
Converting Strings to Numbers
str2num• Converts a string to a number
(double)
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
Plotting Data From Multiple Files
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.
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!