matlab data types, input and output. data types char: >> a = ‘ jim ’ char: >> a =...
TRANSCRIPT
Matlab Data Matlab Data types, input and types, input and
outputoutput
Data typesData types
Char:Char:>> a = >> a = ‘‘JimJim’’
Numeric: Numeric: uint8, uint16, uint32, uint64uint8, uint16, uint32, uint64int8, int16, int32, int64int8, int16, int32, int64singlesingledoubledouble
uint8uint8 The uint8 Data TypeThe uint8 Data Type uintsuints are a type of integer. The u-int-8 stands for: are a type of integer. The u-int-8 stands for:
U:U: Means: UnsignedMeans: Unsigned Which means: All values are positive (no negative Which means: All values are positive (no negative
"sign" allowed)."sign" allowed). INT:INT: Means: IntegersMeans: Integers Which means: All values are whole numbers, Which means: All values are whole numbers,
0,1,2,3...0,1,2,3... 88 Only 8 bits of information:Only 8 bits of information: Which means: the max value is 255 and the min Which means: the max value is 255 and the min
value is 0.value is 0.
Single and Double Single and Double precisionprecision
Double-Precision Floating PointDouble-Precision Floating PointMATLAB constructs the double-precision (or double) data type MATLAB constructs the double-precision (or double) data type according to IEEE® Standard 754 for double precision. Any according to IEEE® Standard 754 for double precision. Any value stored as a double requires 64 bits, formatted as shown in value stored as a double requires 64 bits, formatted as shown in the table below:the table below:
Bits Usage Bits Usage 63 Sign (0 = positive, 1 = negative)63 Sign (0 = positive, 1 = negative)62 to 52 Exponent, biased by 102362 to 52 Exponent, biased by 102351 to 0 Fraction f of the number 1.f51 to 0 Fraction f of the number 1.f
Single-Precision Floating PointSingle-Precision Floating Point MATLAB constructs the single-precision (or single) data type MATLAB constructs the single-precision (or single) data type
according to IEEE Standard 754 for single precision. Any value according to IEEE Standard 754 for single precision. Any value stored as a single requires 32 bits, formatted as shown in the stored as a single requires 32 bits, formatted as shown in the table below:table below:
Bits UsageBits Usage31 Sign (0 = positive, 1 = negative) 31 Sign (0 = positive, 1 = negative) 30 to 23 Exponent, biased by 12730 to 23 Exponent, biased by 12722 to 0 Fraction f of the number 1.f22 to 0 Fraction f of the number 1.f
uint8uint8 The uint8 Data TypeThe uint8 Data Type uintsuints are a type of integer. The u-int-8 stands for: are a type of integer. The u-int-8 stands for:
U:U: Means: UnsignedMeans: Unsigned Which means: All values are positive (no negative Which means: All values are positive (no negative
"sign" allowed)."sign" allowed). INT:INT: Means: IntegersMeans: Integers Which means: All values are whole numbers, Which means: All values are whole numbers,
0,1,2,3...0,1,2,3... 88 Only 8 bits of information:Only 8 bits of information: Which means: the max value is 255 and the min Which means: the max value is 255 and the min
value is 0.value is 0.
uint16uint16 The uint16 Data TypeThe uint16 Data Type uintsuints are a type of integer. The u-int-16 stands for: are a type of integer. The u-int-16 stands for:
U:U: Means: UnsignedMeans: Unsigned Which means: All values are positive (no negative Which means: All values are positive (no negative
"sign" allowed)."sign" allowed). INT:INT: Means: IntegersMeans: Integers Which means: All values are whole numbers, Which means: All values are whole numbers,
0,1,2,3...0,1,2,3... 1616 Only 16 bits of information:Only 16 bits of information: Which means: the max value is 65535 and the min Which means: the max value is 65535 and the min
value is 0.value is 0.
uint32uint32 The uint32 Data TypeThe uint32 Data Type uintsuints are a type of integer. The u-int-32 stands for: are a type of integer. The u-int-32 stands for:
U:U: Means: UnsignedMeans: Unsigned Which means: All values are positive (no negative Which means: All values are positive (no negative
"sign" allowed)."sign" allowed). INT:INT: Means: IntegersMeans: Integers Which means: All values are whole numbers, Which means: All values are whole numbers,
0,1,2,3...0,1,2,3... 3232 Only 32 bits of information:Only 32 bits of information: Which means: the max value is 4294967295 and the Which means: the max value is 4294967295 and the
min value is 0.min value is 0.
int8int8 The int8 Data TypeThe int8 Data Type intsints are a type of integer. The int-8 stands for are a type of integer. The int-8 stands for
signed integers. signed integers.
INT: only integersINT: only integers
Only 8 bits of information: (using the 2’s Only 8 bits of information: (using the 2’s complement technique)complement technique)
Which means: the max value is 127 and the min Which means: the max value is 127 and the min value is -128.value is -128.
int8int8 The int8 Data TypeThe int8 Data Type intsints are a type of integer. The int-8 stands for are a type of integer. The int-8 stands for
signed integers. signed integers.
INT: only integersINT: only integers
Only 8 bits of information: (using the 2’s Only 8 bits of information: (using the 2’s complement technique)complement technique)
Which means: the max value is 127 and the min Which means: the max value is 127 and the min value is -128.value is -128.
int16int16 The int16 Data TypeThe int16 Data Type intsints are a type of integer. The int-19 stands for are a type of integer. The int-19 stands for
signed integers. signed integers.
INT: only integersINT: only integers
Only 16 bits of information: (using the 2’s Only 16 bits of information: (using the 2’s complement technique)complement technique)
Which means: the max value is 32767 and the Which means: the max value is 32767 and the min value is -32768.min value is -32768.
charchar char char character data stored in 2 bytes (16 bits)character data stored in 2 bytes (16 bits)
A = ‘a’A = ‘a’
Strings are stored as a vector of charactersStrings are stored as a vector of characters
B=‘abc’B=‘abc’
C = char(123)C = char(123)
Single and double Single and double precisionprecision
single: single precision numerical data (32 bits in single: single precision numerical data (32 bits in Matlab)Matlab)
A = single(pi); A = single(pi);
double: double precision numerical data (64 bits in double: double precision numerical data (64 bits in Matlab). By default, data are stored as a double Matlab). By default, data are stored as a double precision data in Matlab. precision data in Matlab.
B = double(pi)B = double(pi)
Number formattingNumber formatting Matlab will often display numbers with a given Matlab will often display numbers with a given
number of decimals based on "formatting" number of decimals based on "formatting" options as opposed to their actual value. options as opposed to their actual value.
NoteNote: While I will differentiate between : While I will differentiate between integers and doubles, Matlab will treat most integers and doubles, Matlab will treat most numbers (unless it is explicitly told otherwise) numbers (unless it is explicitly told otherwise) as doubles.as doubles.
This is because, from a scientific viewpoint, This is because, from a scientific viewpoint, doubles are the most accurate representation doubles are the most accurate representation the computer can (typically) use. We will talk the computer can (typically) use. We will talk about the limits of precision on a computer at about the limits of precision on a computer at another time.another time.
Data type conversionData type conversion
A = ‘a’A = ‘a’
B = int8 (A) B = int8 (A)
C = piC = pi
D = char(C)D = char(C)
E = single (D)E = single (D)
D = double(D)D = double(D)
Formatting numbersFormatting numbers You can change this behavior by using the "format" You can change this behavior by using the "format"
command command
>> format >> format longlong >> a >> a a = a = 5.12312312354824 5.12312312354824 >> format >> format shortshort >> a >> a a = a = 5.1231 5.1231
fprintf function (not fprintf function (not covered)covered)
The fprintf function allows you to The fprintf function allows you to "write" information to the screen for "write" information to the screen for the user to view. This the user to view. This veryvery important important when user interaction is involved. when user interaction is involved. The 'f' in printf stands for formatted. The 'f' in printf stands for formatted. This means you can "format" how This means you can "format" how the data is printed in such a manner the data is printed in such a manner as to make it easy to read.as to make it easy to read.
Print informationPrint information
The display command: The display command: disp is a function that will display a disp is a function that will display a valuevalue
name = 'Jim';name = 'Jim';
disp(name);disp(name);
Input functionInput function The input function allows you to ask a user to The input function allows you to ask a user to
type some sort of information into the type some sort of information into the program and to save that information into a program and to save that information into a variable that the program can process.variable that the program can process.
Ask the user a question and wait for a typed Ask the user a question and wait for a typed response. Save the response in a variable:response. Save the response in a variable:
age = input('how old are you: '); age = input('how old are you: '); % At this point, the variable: age, will cont % At this point, the variable: age, will contain ain % whatever value the user types % whatever value the user types
Reading stringsReading strings By default, the input function reads a number, and if you By default, the input function reads a number, and if you
type say, type say, hellohello it will assume that hello is a variable it will assume that hello is a variable containing a number. containing a number. If you really want the string "hello" then you would have If you really want the string "hello" then you would have to type to type 'hello''hello' (tick marks). (tick marks).
To tell a Matlab program to read a string of characters To tell a Matlab program to read a string of characters directly with out having to type the tick marks, you must directly with out having to type the tick marks, you must use the 's' syntax as shown here:use the 's' syntax as shown here:
>>name = input('what is your name: ', 's'); >>name = input('what is your name: ', 's'); % user types jim without tick marks % user types jim without tick marks name = jim name = jim % At this point, the variable: name, will contain % At this point, the variable: name, will contain % whatever value the user types (as a string of charac % whatever value the user types (as a string of characters), ters), % in this case 'jim' % in this case 'jim'
Reading stringsReading strings >>name = input('what is your name: '); >>name = input('what is your name: ');
% user types jim without tick marks % user types jim without tick marks ??? Error ??? Error usingusing ==> input ==> input Undefined Undefined functionfunction or variable 'jim'. or variable 'jim'. >>name = input('what is your name: '); >>name = input('what is your name: '); % user types 'jim' with tick marks % user types 'jim' with tick marks name = name = jim jim