Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS.

Similar presentations


Presentation on theme: "CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS."— Presentation transcript:

1 CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS

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 Spaces are characters too The ‘abc’ symbol indicates a character array

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? >> u = double(T) % double is a built-in function. >> char(u) % performs the opposite function. >> u=double(C)‏ u = 72 101 108 108 111 >> char(u)‏ ans = Hello Example: Conversion between Character and Numeric: Character arrays are similar to vectors, except: Each element contains a single character

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 Variable 8 total spaces 2 after the decimal point floating point format

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

24


Download ppt "CMPS 1371 Introduction to Computing for Engineers CHARACTER STRINGS."

Similar presentations


Ads by Google