Computer Programming with MATLAB
Lesson 7: Data Types
by
Akos Ledeczi and Mike Fitzpatrick
The Limitation of Computers
Real numbers in mathematics:
◦ Can be infinitely large◦ Have infinitely fine resolution
Computers: Finite memory
◦ Upper limit on the largest number that can berepresented
◦ Lower limit on the absolute value of any non-zero
number
The set of values that can be represented by
a MATLAB variable is finite.
Data Types
MATLAB:
many different data types A data type is defined by:
◦ Set of values◦ Set of operations that can be performed on those values
MATLAB:
◦ All elements of a given array must be of the same type◦ Elementary type
Type of a MATLAB array is defined by
◦ Number of dimensions◦ Size in each dimension
◦ Elementary type
Numerical Types
double
◦ Default type in MATLAB◦ Floating point representation
Example: 12.34 = 1234 * 10-2
Mantissa and exponent
◦ 64 bits (8 bytes)
single
◦ 32-bit floating point Integer types
◦ Signed, unsigned◦ 8-, 16-, 32-, 64-bit long
Range of Values
Range of Values
Useful functions
Type check:
◦ class◦ isa
>> isa(x,’double’)
Range check:
◦ intmax, intmin◦ realmax, realmin
>> intmax(’uint32’)
Conversion:
◦ Name of function = name of desired data type◦ int8(x), uint32(x), double(x), etc.
Operators
Arithmetic operators
◦ Operands of the same data type: No problem◦ Different data types:
“mixed-mode arithmetic”
Many rules and restrictions
Relational operators
◦ Different data types are always allowed◦ Result is always of logical type
Strings
Text: string
We have used them already:
◦ Argument to fprintf and other functions String: vector of char-s
Numerical type
◦ Uses an encoding scheme◦ Each character is represented by a number
◦ ASCII scheme
ASCII code
American Standard Code
for Information
Interchange
Developed in the1960’s
7 bits, 128 characters:
◦ Latin alphabet
◦ Digits
◦ Punctuation
◦ Special characters
Newer schemes with far
more characters
ASCII is a subset of them
Exercise
Print out the visible characters of the ASCII
table:
function char_codes
for ii = 33:126
fprintf('%s',char(ii));
end
fprintf('\n');
String functions
Structs
An array must be homogeneous:
◦ It cannot contain elements of multiple types. A struct can be heterogeneous:
◦ It can contain multiple types. A struct is different from an array:
◦ fields, not elements◦ field names, not indices
◦ Fields in the same struct can have different types.
Versatility inside:
◦ A field of a struct can contain another struct.Structs in action
>> r.ssn = 12345678
r =
ssn: 12345678
>> class(r)
ans =
struct
>> class(r.ssn)
ans =
double
>> r.name = 'Homer Simpson'
r =
ssn: 12345678
name: 'Homer Simpson‘
>> r.address.street = '742 Evergreen Terrace'
r =
ssn: 12345678
name: 'Homer Simpson'
address: [1x1 struct]
Structs
An array must be homogeneous:
◦ It cannot contain elements of multiple types.◦ It can contain multiple types.
A struct is different from an array:
◦ fields, not elements◦ field names, not indices
◦ Fields in the same struct can have different types.
Versatility inside:
◦ A field of a struct can contain another struct.◦ Structs can hold arrays, and arrays can hold structs.
String functions
Pointers
How to store a page of text?
◦ Each line should be a separate string◦ Cannot use an array of chars:
Each line would have to have the same length
◦ A vector of objects with each referring to one line
Pointer
◦ Each variable (scalar, vector, array, etc.) is stored inthe computer memory.
◦ Each memory location has a unique address.
◦ A pointer is a variable that stores an address.
◦ MATLAB calls a pointer a “cell”.
Cells
MATLAB has a restrictive pointer model
◦ Strict rules on what can be done with cells◦ Harder to make mistakes
But it is a powerful way to store
heterogeneous data
◦ Cell arrays ◦ Used more frequently than structs
New syntax:
◦ To access the data a cell points to, use: { }Cell array example
>> p = cell(2,3)
p =
[] [] []
[] [] []
>>
Cell array example
>> p = cell(2,3)
p =
[] [] []
[] [] []
>> p{2,1} = pi
p =
[] [] []
[3.14] [] []
>>
Cell array example
>> p = cell(2,3)
p =
[] [] []
[] [] []
>> p{2,1} = pi
p =
[] [] []
[3.14] [] []
>> p{1,1} = int8(-17)
p =
[-17] [] []
[3.14] [] []
>>
Cell array example
>> p = cell(2,3)
p =
[] [] []
[] [] []
>> p{2,1} = pi
p =
[] [] []
[3.14] [] []
>> p{1,1} = -17
p =
[-17] [] []
[3.14] [] []
>> p{2,2} = 'Awesome'
P =
[-17] [] []
[3.14] 'Awesome'[]
Cell array example
>> p{2,3} = [2 4; 6 8; 10 12]
P =
[-17] [] []
[3.14] ‘Awesome’[3x2 double]
>>
Cell array example
>> p{2,3} = [2 4; 6 8; 10 12]
P =
[-17] [] []
[3.14] ‘Awesome’[3x2 double]
>> p{1,3} = sum(p{2,3});
>>
Cell array example
>> p{2,3} = [2 4; 6 8; 10 12]
P =
[-17] [] []
[3.14] ‘Awesome’[3x2 double]
>> p{1,3} = sum(p{2,3});
>> P{1,2} = 1/0;
>>
Cell array example
>> p{2,3} = [2 4; 6 8; 10 12]
P =
[-17] [] []
[3.14] ‘Awesome’[3x2 double]
>> p{1,3} = sum(p{2,3});
>> P{1,2} = 1/0;
>> class(p)
ans =
cell
>>
Cell array example
>> p{2,3} = [2 4; 6 8; 10 12]
P =
[-17] [] []
[3.14] ‘Awesome’[3x2 double]
>> p{1,3} = sum(p{2,3});
>> P{1,2} = 1/0;
>> class(p)
ans =
cell
>> class(p{1,2})
ans =
double
>>
Cell array example
>> p{2,3} = [2 4; 6 8; 10 12]
P =
[-17] [] []
[3.14] ‘Awesome’[3x2 double]
>> p{1,3} = sum(p{2,3});
>> P{1,2} = 1/0;
>> class(p)
ans =
cell
>> class(p{1,2})
ans =
double
>> class(p(1,2))
ans =
cell
>>
Cell array example
>> p{2,3}(3,2)
ans =
12
>>