Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Chapter 7

2 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 Advanced National Seismic System (ANSS) http://www.ncedc.org/anss/ Near real-time data for all global earthquake events! Not just numbersNot just numbers USGS Current Water Data http://waterdata.usgs.gov/nwis/rt Near real-time data for streams in all 50 states! Not just numbersNot just numbers

4 Recall that strings are arrays of characters i.e. a matrix of characters

5 Strings can form rectangular matrices too! Must follow same matrix rules Rows and Cols must be consistent

6 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 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 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 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 Attaway also covers deblank char strtrim upper lower These may come in handy someday You can read about these functions for their usage

11 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 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 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 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 str2num Converts a string to a number (double)

16 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

18 Fair Warning: Data files are read in the order that ls reports. If not in order, you will have to sort the data.

19


Download ppt "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."

Similar presentations


Ads by Google