CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING
Data types So far we have seen the following data types: Double Char String Other?
The class function What is a class? A class is the blueprint from which individual objects are created. Ex. >> x = 14 x = 14 >> class(x) ans = double
Numbers
What’s the min? What’s the max? >> intmax('int32') ans = >> intmin('int32') ans = >> realmax('single') ans = e+038 >> realmin('single') ans = e-038
Changing types From int to double>> y = x y = 10 >> class(y) ans = Int8 >> y = double(y) y = 10 >> class(y) ans = double
More changes >> int8(9.4) ans = 9 >> int8(-9.4) ans = -9 >> int8(-9.5) ans = -10 >> int8(128) ans = 127 >> int8(-1000) ans = -128
Ascii characters
Strings and functions >> book_title = 'Matlab for Smarties‘ >> length(book_title) >> book_title(1) >> book_title(4:16) >> double(book_title) >> s = 'Two is company, three a crowd.'; >> ssub = s(13:end) >> a = 'Three, two, one'; b = ', '; c = 'BLASTOFF!'; >> count_down = [a,b,c]
More string functions >> x = pi; >> fprintf('x:\nto three decimal places: %6.3f\n', x); x: to three decimal places: >> s=sprintf('x:\nto three decimal places: %6.3f\n', x); >> s
Even more… functions char converts type to char findstr finds the positions of a substring in a string (see also strfind) ischar returns 1 if argument is a character array and 0 otherwise isletter finds letters in string isspace finds spaces, newlines, and tabs in string isstrprop finds characters of specified type in string num2str converts number to string length determines the number of characters in string lower converts letters in string to lower case
Even more… functions regexp finds substrings defined by a regular expression sprintf writes formatted data to string (cf. fprintf) strcat concatenates strings strmatch search array for rows that begin with specified string strncmp applies strcmp to the first n characters of strings strcmp returns 1 if two strings are equal and 0 otherwise strcmpi like strcmp but independent of case strncmp compares first n characters of strings strncmpi like strncmp but independent of case str2num converts string to number upper converts string to upper case
QUESTIONS??