Presentation is loading. Please wait.

Presentation is loading. Please wait.

Matlab Data types, input and output. Data types Char: >> a = ‘ Jim ’ Char: >> a = ‘ Jim ’ Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64.

Similar presentations


Presentation on theme: "Matlab Data types, input and output. Data types Char: >> a = ‘ Jim ’ Char: >> a = ‘ Jim ’ Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64."— Presentation transcript:

1 Matlab Data types, input and output

2 Data types Char: >> a = ‘ Jim ’ Char: >> a = ‘ Jim ’ Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64 single double Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64 single double

3 uint8 The uint8 Data Type The uint8 Data Type uints are a type of integer. The u-int-8 stands for: U: uints are a type of integer. The u-int-8 stands for: U: Means: Unsigned Means: Unsigned Which means: All values are positive (no negative "sign" allowed). Which means: All values are positive (no negative "sign" allowed). INT: INT: Means: Integers Means: Integers Which means: All values are whole numbers, 0,1,2,3... Which means: All values are whole numbers, 0,1,2,3... 8 Only 8 bits of information: Only 8 bits of information: Which means: the max value is 255 and the min value is 0. Which means: the max value is 255 and the min value is 0.

4 Single and Double precision Double-Precision Floating Point MATLAB constructs the double-precision (or double) data type according to IEEE® Standard 754 for double precision. Any value stored as a double requires 64 bits, formatted as shown in the table below: Double-Precision Floating Point MATLAB constructs the double-precision (or double) data type according to IEEE® Standard 754 for double precision. Any value stored as a double requires 64 bits, formatted as shown in the table below: Bits Usage 63 Sign (0 = positive, 1 = negative) 62 to 52 Exponent, biased by 1023 51 to 0 Fraction f of the number 1.f Bits Usage 63 Sign (0 = positive, 1 = negative) 62 to 52 Exponent, biased by 1023 51 to 0 Fraction f of the number 1.f Single-Precision Floating Point Single-Precision Floating Point MATLAB constructs the single-precision (or single) data type according to IEEE Standard 754 for single precision. Any value stored as a single requires 32 bits, formatted as shown in the table below: MATLAB constructs the single-precision (or single) data type according to IEEE Standard 754 for single precision. Any value stored as a single requires 32 bits, formatted as shown in the table below: Bits Usage 31 Sign (0 = positive, 1 = negative) 30 to 23 Exponent, biased by 127 22 to 0 Fraction f of the number 1.f Bits Usage 31 Sign (0 = positive, 1 = negative) 30 to 23 Exponent, biased by 127 22 to 0 Fraction f of the number 1.f

5 uint8 The uint8 Data Type The uint8 Data Type uints are a type of integer. The u-int-8 stands for: U: uints are a type of integer. The u-int-8 stands for: U: Means: Unsigned Means: Unsigned Which means: All values are positive (no negative "sign" allowed). Which means: All values are positive (no negative "sign" allowed). INT: INT: Means: Integers Means: Integers Which means: All values are whole numbers, 0,1,2,3... Which means: All values are whole numbers, 0,1,2,3... 8 Only 8 bits of information: Only 8 bits of information: Which means: the max value is 255 and the min value is 0. Which means: the max value is 255 and the min value is 0.

6 uint16 The uint16 Data Type The uint16 Data Type uints are a type of integer. The u-int-16 stands for: U: uints are a type of integer. The u-int-16 stands for: U: Means: Unsigned Means: Unsigned Which means: All values are positive (no negative "sign" allowed). Which means: All values are positive (no negative "sign" allowed). INT: INT: Means: Integers Means: Integers Which means: All values are whole numbers, 0,1,2,3... Which means: All values are whole numbers, 0,1,2,3... 16 16 Only 16 bits of information: Only 16 bits of information: Which means: the max value is 65535 and the min value is 0. Which means: the max value is 65535 and the min value is 0.

7 uint32 The uint32 Data Type The uint32 Data Type uints are a type of integer. The u-int-32 stands for: U: uints are a type of integer. The u-int-32 stands for: U: Means: Unsigned Means: Unsigned Which means: All values are positive (no negative "sign" allowed). Which means: All values are positive (no negative "sign" allowed). INT: INT: Means: Integers Means: Integers Which means: All values are whole numbers, 0,1,2,3... Which means: All values are whole numbers, 0,1,2,3... 32 32 Only 32 bits of information: Only 32 bits of information: Which means: the max value is 4294967295 and the min value is 0. Which means: the max value is 4294967295 and the min value is 0.

8 int8 The int8 Data Type The int8 Data Type ints are a type of integer. The int-8 stands for signed integers. ints are a type of integer. The int-8 stands for signed integers. INT: only integers INT: only integers Only 8 bits of information: (using the 2’s complement technique) Only 8 bits of information: (using the 2’s complement technique) Which means: the max value is 127 and the min value is -128. Which means: the max value is 127 and the min value is -128.

9 int8 The int8 Data Type The int8 Data Type ints are a type of integer. The int-8 stands for signed integers. ints are a type of integer. The int-8 stands for signed integers. INT: only integers INT: only integers Only 8 bits of information: (using the 2’s complement technique) Only 8 bits of information: (using the 2’s complement technique) Which means: the max value is 127 and the min value is -128. Which means: the max value is 127 and the min value is -128.

10 int16 The int16 Data Type The int16 Data Type ints are a type of integer. The int-19 stands for signed integers. ints are a type of integer. The int-19 stands for signed integers. INT: only integers INT: only integers Only 16 bits of information: (using the 2’s complement technique) Only 16 bits of information: (using the 2’s complement technique) Which means: the max value is 32767 and the min value is - 32768. Which means: the max value is 32767 and the min value is - 32768.

11 char char char character data stored in 2 bytes (16 bits) A = ‘a’ Strings are stored as a vector of characters B=‘abc’ C = char(123)

12 Single and double precision single: single precision numerical data (32 bits in Matlab) A = single(pi); double: double precision numerical data (64 bits in Matlab). By default, data are stored as a double precision data in Matlab. B = double(pi)

13 Number formatting Matlab will often display numbers with a given number of decimals based on "formatting" options as opposed to their actual value. Matlab will often display numbers with a given number of decimals based on "formatting" options as opposed to their actual value. Note: While I will differentiate between integers and doubles, Matlab will treat most numbers (unless it is explicitly told otherwise) as doubles. Note: While I will differentiate between integers and doubles, Matlab will treat most numbers (unless it is explicitly told otherwise) as doubles. This is because, from a scientific viewpoint, doubles are the most accurate representation the computer can (typically) use. We will talk about the limits of precision on a computer at another time. This is because, from a scientific viewpoint, doubles are the most accurate representation the computer can (typically) use. We will talk about the limits of precision on a computer at another time.

14 Data type conversion A = ‘a’ B = int8 (A) C = pi D = char(C) E = single (D) D = double(D)

15 Formatting numbers You can change this behavior by using the "format" command >> format long >> a a = 5.12312312354824 >> format short >> a a = 5.1231 You can change this behavior by using the "format" command >> format long >> a a = 5.12312312354824 >> format short >> a a = 5.1231

16 fprintf function (not covered) The fprintf function allows you to "write" information to the screen for the user to view. This very important when user interaction is involved. The 'f' in printf stands for formatted. This means you can "format" how the data is printed in such a manner as to make it easy to read. The fprintf function allows you to "write" information to the screen for the user to view. This very important when user interaction is involved. The 'f' in printf stands for formatted. This means you can "format" how the data is printed in such a manner as to make it easy to read.

17 Print information The display command: disp is a function that will display a value The display command: disp is a function that will display a value name = 'Jim'; name = 'Jim'; disp(name); disp(name);

18 Input function The input function allows you to ask a user to type some sort of information into the program and to save that information into a variable that the program can process. The input function allows you to ask a user to type some sort of information into the program and to save that information into a variable that the program can process. Ask the user a question and wait for a typed response. Save the response in a variable: Ask the user a question and wait for a typed response. Save the response in a variable: age = input('how old are you: '); % At this point, the variable: age, will contain % whatever value the user types age = input('how old are you: '); % At this point, the variable: age, will contain % whatever value the user types

19 Reading strings By default, the input function reads a number, and if you type say, hello it will assume that hello is a variable containing a number. If you really want the string "hello" then you would have to type 'hello' (tick marks). By default, the input function reads a number, and if you type say, hello it will assume that hello is a variable containing a number. If you really want the string "hello" then you would have to type 'hello' (tick marks). To tell a Matlab program to read a string of characters directly with out having to type the tick marks, you must use the 's' syntax as shown here: To tell a Matlab program to read a string of characters directly with out having to type the tick marks, you must use the 's' syntax as shown here: >>name = input('what is your name: ', 's'); % user types jim without tick marks name = jim % At this point, the variable: name, will contain % whatever value the user types (as a string of characters), % in this case 'jim' >>name = input('what is your name: ', 's'); % user types jim without tick marks name = jim % At this point, the variable: name, will contain % whatever value the user types (as a string of characters), % in this case 'jim'

20 Reading strings >>name = input('what is your name: '); % user types jim without tick marks ??? Error using ==> input Undefined function or variable 'jim'. >>name = input('what is your name: '); % user types 'jim' with tick marks name = jim >>name = input('what is your name: '); % user types jim without tick marks ??? Error using ==> input Undefined function or variable 'jim'. >>name = input('what is your name: '); % user types 'jim' with tick marks name = jim


Download ppt "Matlab Data types, input and output. Data types Char: >> a = ‘ Jim ’ Char: >> a = ‘ Jim ’ Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64."

Similar presentations


Ads by Google