1 / 19

String Manipulation

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)

louisa
Download Presentation

String Manipulation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. String Manipulation Chapter 7

  2. 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) • 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

  3. Common Earth Science Data Sets Advanced 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

  4. Strings: A Quick Review • Recall that strings are arrays of characters • i.e. a matrix of characters

  5. Strings: A Quick Review • Strings can form rectangular matrices too! • Must follow same matrix rules • Rows and Cols must be consistent

  6. 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”

  7. Formatted Strings: sprintf What 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

  8. Custom Plot Labels Using sprintf What 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

  9. Custom Plot Labels Using sprintf What 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

  10. 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

  11. Comparing Strings What 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

  12. Comparing Strings What 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”.

  13. “is*” Functions Now 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

  14. Converting Numbers to Strings int2str • 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

  15. Converting Strings to Numbers str2num • Converts a string to a number (double)

  16. Practical Example:Reading Data Files What 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

  17. Plotting Data From Multiple Files

  18. 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.

  19. 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!

More Related