240 likes | 412 Views
CHARACTER STRINGS. CMPS 1371 Introduction to Computing for Engineers. Character and String Data. Character arrays store character information A character array is produced from a string. Characters. Any string represents a character array in MATLAB
E N D
CHARACTER STRINGS CMPS 1371Introduction to Computing for Engineers
Character and String Data • Character arrays store character information • A character array is produced from a string
Characters • Any string represents a character array in MATLAB • Each character requires 2 bytes of storage
The ‘abc’ symbol indicates a character array Spaces are characters too
How are characters stored in MATLAB? All information in computers is stored using a series of zeros and ones ASCII – Used in small computers EBCDIC – Used in mainframes and super computers You can think of this list of 0’s and 1’s as a base two number
Character and String Data Every character stored using ASCII or EBCDIC code has both a binary representation and a decimal equivalent When we ask MATLAB to change a character to a double, the number we get is the decimal equivalent in the ASCII coding system
MATLAB includes functions to change data types Use the double function to convert to a double precision floating point number Also use: >> uint8(‘a’) Use char to convert a number to a character
How are Characters Stored? Character arrays are similar to vectors, except: Each element contains a single character Conversion between Character and Numeric: >> u = double(T) % double is a built-in function. >> char(u) % performs the opposite function. Example: >> u=double(C) u = 72 101 108 108 111 >> char(u) ans = Hello
Compare Strings Strings are translated into vectors of numbers (ASCII) Use any logical operators However: Must be the same length >> 'fred' == 'brad' 0 1 0 1 >> 'fred' == 'george' % produces error since different lengths ??? Error using ==> eq
String Comparison Matlab provides a function to compare any set of strings strcmp('string1' , 'string2') Returns: 1 (true) if strings are identical 0 (false) if strings are not identical >> strcmp('fred', 'george') ans = 0 Also use isequal(x,y)
Output Options Enter the name of a variable Use the disp function Use the fprintf function Also use sprintf function
Display The display (disp) function can be used to display the contents of a matrix or string
Display The disp function only takes one input you must combine arrays to make more complicated output Use the num2str(x) function to change numeric information to a string disp(['The values in the x array are: ' num2str(x)])
Although these characters look like numbers, they are interpreted by the computer as part of a character array – they no longer have any numeric meaning Notice that the ans matrix is listed as a character array
Display If you want to include an apostrophe in a string, you need to enter the apostrophe twice. If you don’t, MATLAB thinks the apostrophe terminates the string. For example: disp('The moon''s gravity is 1/6th that of the earth')
Formatted Output fprintf gives you more control over your output than the disp function You can combine text and numbers You can control how many digits to display, and their position
fprintf fprintf('text to display %#.#f \n' variable); %f – displays fixed point or decimal %s – displays string variables %e – displays exponential notation \n – new line \t – tab \f – form feed (new line, same position)
8 total spaces2 after the decimal pointfloating point format Variable
X is a matrix /n is a carriage return
fprintf If you want to include a percentage sign in an fprintf statement, you need to enter the % twice. If you don’t, MATLAB thinks the % is a placeholder for data. For example: fprintf('The interest rate is %5.2f %% \n', 5) results in: The interest rate is 5.00 %
For example… The US Naval Academy requires applicants to be at least 5’6” tall Consider this list of applicant heights 63”, 67”, 65”, 72”, 69”, 78”, 75” Which applicants meet the criteria?
The find function returns the index number for elements that meet a criteria index numbers element values