130 likes | 263 Vues
This document explores essential data types utilized in scientific and engineering programming, including doubles, chars, and strings. You will learn about class functions, type conversions, and the minimum and maximum values of various data types. Additionally, it covers string manipulations, ASCII characters, and practical functions in MATLAB. Whether you're changing types or working with string functions, this guide provides a comprehensive overview for beginners and intermediate programmers alike to enhance their understanding of programming data structures.
E N D
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
What’s the min? What’s the max? • >> intmax('int32') • ans = • 2147483647 • >> intmin('int32') • ans = • -2147483648 • >> realmax('single') • ans = • 3.4028e+038 • >> realmin('single') • ans = • 1.1755e-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
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: 3.142 >> s=sprintf('x:\nto three decimal places: %6.3f\n', x); >> s
Even more… functions • char converts type to char • findstrfinds the positions of a substring in a string (see also strfind) • ischarreturns 1 if argument is a character array and 0 otherwise • isletterfinds letters in string • isspacefinds spaces, newlines, and tabs in string • isstrpropfinds 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 • regexpfinds substrings defined by a regular expression • sprintfwrites formatted data to string (cf. fprintf) • strcatconcatenates strings • strmatchsearch array for rows that begin with specified string • strncmpapplies strcmp to the first n characters of strings • strcmpreturns 1 if two strings are equal and 0 otherwise • strcmpilike strcmp but independent of case • strncmpcompares first n characters of strings • strncmpilike strncmp but independent of case • str2num converts string to number • upper converts string to upper case