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 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 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 • Recall that strings are arrays of characters • i.e. a matrix of characters • Strings can form rectangular matrices too! • Must follow same matrix rules • Rows and Cols must be consistent • 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” 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 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 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 • Attaway also covers • deblank • char • strtrim • upper • lower • These may come in handy someday • You can read about these functions for their usage 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 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”. 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 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 str2num • Converts a string to a number (double) 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 Fair Warning: • Data files are read in the order that ls reports. • If not in order, you will have to sort the data.