Advanced Topic- Data Types. Introduction to MATLAB 7 Engineering 161. Data Types- Chapter 10. To take advantage of more of MATLAB’s capabilities we need to learn about data types.
Advanced Topic- Data Types
Introduction to MATLAB 7
We have some familiarity with floating point
representations, character strings and logical
data types. Let’s look at data types in more detail.
It really depends on what you want to do, that is, the kinds of calculations or manipulations that you want to perform. Often integer arithmetic is sufficient and it runs faster than double precision arithmetic, or if you want to manipulate strings of characters in a program, you need to know how to define character strings and manipulate them.
x = int32(10);
the number 10 would be assigned to x and stored as signed integer using 32 bits in 2’s complement form
z = 5 + 3*i where 5 would be the real part and 3 the imaginary part of the complex number. Use the operations +, -, / and * to manipulate complex numbers.
v = 5 + 3*i;% the * is optional in the
z = 2 – i*1;% imaginary part, I like to use it.
v*z% recall that i2 = -1
12 + i
H = ‘Holly’; % this is a character string
H becomes a character array where each character in the string, including spaces, is a separate element in a character array. H(5) would be the character y.
MATLAB uses ASCII to represent characters, which is an 8 bit coding scheme.
L = sym(‘x^2 – 2’);
L = x^2 -2;
x= 5, y = -3;
x > y
names = [‘Holly’; ‘Sam’; ‘Judy’];
This would generate an error message as MATLAB requires all the rows to have the same number of characters, clearly violated here.
One way to get around this is to pad the strings so they have the same length,
names = [‘Holly’; ‘ Sam’; ‘ Judy’];
or use the char function
names = char(‘Holly’, ‘Sam’, ‘Judy’); produces a column array of the names.
Don’t forget the num2str function if you want to combine character strings and numbers into the same array. Different types rarely mix in MATLAB.