1 / 24

CMPS 1371 Introduction to Computing for Engineers

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

sheena
Download Presentation

CMPS 1371 Introduction to Computing for Engineers

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. CHARACTER STRINGS CMPS 1371Introduction to Computing for Engineers

  2. Character and String Data • Character arrays store character information • A character array is produced from a string

  3. Characters • Any string represents a character array in MATLAB • Each character requires 2 bytes of storage

  4. The ‘abc’ symbol indicates a character array Spaces are characters too

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

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

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

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

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

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

  11. Output Options Enter the name of a variable Use the disp function Use the fprintf function Also use sprintf function

  12. Display The display (disp) function can be used to display the contents of a matrix or string

  13. 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)])‏

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

  15. 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')

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

  17. 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)‏

  18. 8 total spaces2 after the decimal pointfloating point format Variable

  19. X is a matrix /n is a carriage return

  20. 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 %

  21. 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?

  22. The find function returns the index number for elements that meet a criteria index numbers element values

  23. Create a more readable report

More Related