left-icon

MATLAB Succinctly®
by Dmitri Nesteruk

Previous
Chapter

of
A
A
A

CHAPTER 2

Data Types

Data Types


We are going to begin our relationship with MATLAB by looking at the different numeric types that MATLAB supports. We will also discuss some of the predefined constants that are available to users when working with numeric data, and then we will then discuss the various formats in which numeric data can be printed in the command window. Subsequently, we shall discuss character strings—these are not as important as numbers, but they are still used in many places within MATLAB. We will then look at structures, which provide a great way of effortlessly grouping related data together. Finally, we will talk about the more obscure concept of cell arrays, and finish the chapter off with a discussion of function handles.

One thing I would like to point out here is that an in-depth discussion of arrays and matrices is postponed, because I am going to dedicate a whole chapter to them. However, I will briefly mention arrays in this chapter.

Numeric Types

MATLAB supports various data types, and of most importance to us are obviously the numeric types, i.e. data types used to store numbers. In MATLAB's terminology, a data type is called a class, but to avoid confusing those of my readers who are already practicing programmers, I will keep using the term data type whenever I can.

There are two ways of getting the class, or the data type, of something. The first is to use the class function, which yields just the data type.

>> x=5; class(x)

ans =
double

The semicolon in this example is used to separate two statements on a single line, and also suppresses the output from the first statement (x=5). Don’t worry, we’ll explain it all a bit later.

Alternatively, you can get even more info by using the whos command. This command lists not just the type of the variable but also its name, size, and other attributes:

>> whos x

  Name      Size            Bytes  Class     Attributes
  x         1x1                 8  double

So the first thing to note is that the default data type for numeric values in MATLAB is double-precision. We can verify this by using the class function. For example, if we type class(5) we get a result of double. If we want single-precision instead, we can use the single function:

>> y = single(5); class(y)
ans =
single

But what does single and double precision mean? Well, essentially, it means that both of these types can store real numbers, i.e. numbers with whole and fractional parts, and the difference between single and double is how much memory they take.

>> whos x, whos y

  Name      Size            Bytes  Class     Attributes
  x         1x1                 8  double             
  Name      Size            Bytes  Class     Attributes
  y         1x1                 4  single

As you can see, the double-precision number takes up twice as many bytes (8 as opposed to 4 for a single-precision number), so it can store larger values or values with higher precision. For example, here’s how the representation of π varies depending on data type:

>> single(pi)
ans =
        3.141593
>> pi
ans =
          3.14159265358979

If you want to know the limits of those values, you can use the realmin and realmax functions to figure out the minimum and maximum values that can be stored.

>> realmin('single'), realmax('double')

ans =
  1.1755e-38
ans =
  1.7977e+308

MATLAB also supports integer data types, i.e. data types which only contain a whole part of the number. For example, if you load an image, each pixel of an image might have red, green, and blue values ranging from 0 to 255. If you need to define integers yourself, conversions are made using the int and uint functions for signed and unsigned integers respectively. Each function is postfixed by the number of bits, so for example, to get a 64-bit unsigned integer with a value of 5 you can write:

>> z = uint64(5), whos z

z =
                    5
  Name      Size            Bytes  Class     Attributes
  z         1x1                 8  uint64

Both signed and unsigned integers are available in 8, 16, 32, and 64-bit precision.

Tip: If you do not know what data types to work with, it's best to stick to double-precision. Double-precision operations are typically fast enough for most purposes, though single-precision operations can be faster, especially if you are performing GPU computation (which, by the way, MATLAB also supports using the optional Parallel Computing Toolbox).

Inf, NaN, and Other Constants

If we divide 1 by 0, we get infinity, which is represented in MATLAB with the inf constant. You can use inf in your own calculations, for example in symbolic calculations where you calculate the limit when something goes to infinity. You can use the isinf function to test if something is equal to infinity.

inf is an example of a predefined MATLAB constant. There aren't many such constants in MATLAB, but there are a few that we need to discuss.

Apart from inf, there is nan, short for “not a number.” This constant represents a value that is neither real nor complex. Expressions such as inf/inf, 0/0 or any arithmetic expressions involving a nan yield a nan. You can use the isnan function to test for this. Note that, unlike inf, you cannot test for a nan using comparison operators: both == and ~= will yield false.

Tip: inf and nan are actually array-generating functions with different parameter overloads; for example, writing inf(2,3) creates a 2×3 array of inf values. The way to detect this is by noting that, if you write whos inf, you don’t get any output. And calling open inf will open the appropriate function file… or, at least, some documentation.

MATLAB has a few other constants. First, there are limit constants for maximum and minimum integers and floating-point numbers. For example, to get the maximum possible integer we can use the intmax constant.

Then there are predefined constants such as pi and i/j. pi ( π ) has a predictable value, 3.14…, but being a constant (rather than a function), it can be redefined—probably not a good idea, though. If you look at the constants i and j (these are actually functions, so you can declare your own variables with these names), you will see that their representation is somewhat different than what we have seen previously.

>> i

ans =
       0        +    1i

In fact, doing a whos on the result tells us that, while it has a class of double, it is also a complex number:

>> whos ans

Name      Size            Bytes  Class     Attributes
ans       1x1                16  double    complex

Unlike a double-precision value, the complex number in this example is a combination of two double-precision values (one real and one imaginary), so the variable takes 16 bytes instead of the usual eight. Note that complex numbers can also use single-precision values instead of double-precision ones. MATLAB’s use of complex numbers is pervasive, so if you take a square root of a negative number or try to solve a quadratic equation that has complex roots, MATLAB will give you complex results rather than showing an error:

>> sqrt(-2)
ans =
   0.0000 + 1.4142i

Finally, MATLAB has the constant eps, which represents the relative floating-point accuracy, i.e. the distance to the next largest floating-point value.

Numeric Output Formats

Let us make a sufficiently big number, say 1010:

>> 10^10

ans =
   1.0000e+10

As you can see in the output, the number is presented using engineering notation. We can customize the way that numbers are output by using the format command. For example, to avoid the engineering notation for this number, I can specify the format as longg. This lets me see the number fully:

>> format longg
>> 10^10

ans =
               10000000000

Alternatively, we can tell MATLAB to use the currency format, which leaves only two decimal places. On the other hand, we can, for example, ensure that all numeric values are output in hexadecimal format.

>> format bank
>> ans

ans =
10000000000.00

>> format hex
>> ans

ans =
   4202a05f20000000

MATLAB also supports a formatting style that keeps fractions in ratio format, so instead of evaluating the numeric value of a division, it tries to keep the result as a fraction.

>> format rat
>> 2*3/7+1/12

ans =
      79/84

Note however that this does not imply symbolic evaluation, and dividing pi by 2 will yield a fraction that is sufficiently close to the real value of pi over 2.

>> pi/2

ans =
     355/226

For a full list of available numeric formats, check out the documentation section of the format command. This contains a list of every supported format string that is available in MATLAB. Please note that the format command only affects the representation of a number, and not the value that is actually stored. Just because MATLAB is showing a number to two decimal digits does not mean that the rest of it was suddenly lost. All the information is still there—you just do not see it.

Arrays

We have a whole chapter on the subject of arrays ahead of us so, to keep things brief, here is a small introduction.

Imagine you need to store information about several successive measurements. Sure, you could give each of those measurements its own variable (a, b, c, and so on) but this is rather impractical. Instead, what you can do is to declare an array—a structure specifically designed to store several values. Arrays are declared and initialized with square brackets, for example:

>> values = [1 2 3]

values =
     1     2     3

To access an element of an array, you put the element’s index right after the array name in round brackets. Unlike in some programming languages, array indices start with 1. The round brackets let you both read array elements and change their values:

>> values(2)
ans =
     2

>> values(3) = 15
values =
     1     2    15

Arrays form the basis of pretty much everything that happens in MATLAB.

Characters and Strings

When it comes to computation, we are mainly concerned with numeric data, but we also sometimes need to use textual data. For this, we use character strings, which are simply arrays of characters.

Strings are defined by delimiting text with single quotation marks:

>> s = 'hello'

s =
hello

To make a single quote part of a string, you just type it twice:

>> 'let''s go'

ans =
let's go

Let's use the whos command to see what we just made.

>> whos ans

Name      Size            Bytes  Class    Attributes
  ans       1x8                16  char

As you can see, a string in our case is simply an array of eight characters. MATLAB uses fixed-length Unicode characters of two bytes each, thus our eight-character string actually takes up 16 bytes of memory.

You can, of course, access individual string characters. For example, to get the first character, you can write ans(1). Note that the data type of the result is still char:

>> ans(1); class(ans)

ans =
char

The simplest way of joining strings together is to simply use square brackets around the parts to join:

>> a = 'hello';
>> b = 'world';
>> [a ', ' b '!']

ans =
hello, world!

Note however that attempting to include numeric values into the concatenation can lead to unforeseen consequences, such as having your numeric value converted to a character symbol with the corresponding ASCII code.

>> s = 'test'

s =
test

>> s(1) + 'a'

ans =
   213

The reverse is useful though: for example, to quickly get a line break between two lines of text, you can rely on MATLAB to convert the number 10 into the character code for a line break. Thus, adding a line break between two pieces of text is as simple as:

>> ['hello' 10 'world']
ans =
hello
world

To get numeric data in text form, simply use the num2str function:

>> age = 30;
>> ['I am ' num2str(age) ' years old']

ans =
I am 30 years old

One place where strings are used is in writing something to the command window. For that, we can use the disp function:

>> disp(ans)

I am 30 years old

Strings are used in many places. For example, to plot a graph of x∙sinx2 (we’ll talk about plots in detail a little bit later), you can provide the function as a string:

>> ezplot('sqrt(x) * sin(x^2)')

MATLAB has lots of functions for string manipulation and conversion. For example, you get string comparison functions strcmp and strcmpi for case-sensitive and insensitive comparisons:

>> strcmpi('abc', 'ABC')

ans =
     1

In practice, though, you are unlikely to need any of these functions that often.

Structures

Let’s say that in our MATLAB program we need to store some data about a person, such as their name and age. We can certainly use two variables for this or, as an alternative, we can define a structure. Here is what it looks like:

>> person.name = 'john';
>> person.age = 22

person =
    name: 'john'
     age: 22

How is this possible? What does it mean? Well, there's a bit of magic going on here. As you may have noticed already, MATLAB is an environment where you don’t need to declare values before assigning values to them. For example, writing x=2 automatically creates a variable called x. The same principle is at play in the above code listing.

The idea is that when we wrote person.name = 'john', we effectively created a structure called person and we created a field called name in that structure. A structure array can be thought of as a set of values that are addressed using the dot (.) operator. Note that, unlike in a typical programming language, we didn't have to declare the structure beforehandwe just start using it. And it may seem as though person is just one entity, but in actual fact, it's an array that has just one element. Here's the proof:

>> person(1)

ans =
    name: 'Sherlock'
     age: 60

The fields of a structure can be practically anything, and indeed a field can itself be a structure. For example, our structure can have an address field, itself a structure with a house number and street name. We can then assign this address to a person's address.

>> address.houseNumber = '221B';
>> address.streetName = 'Baker st.';
>> person.address = address

person =
       name: 'Sherlock'
        age: 60.00
    address: [1x1 struct]

And, of course, person.address is not just a single structure; it is, as all things in MATLAB, an array of elements. And we can dynamically add another address to the person's address field, for example:

>> person.address(2).houseNumber = '8-9';
>> person.address(2).streetName = 'Hyde Park Pl'

person =
       name: 'Sherlock'
        age: 60
    address: [1x2 struct]

Cell Arrays

Before we begin talking about cell arrays, there's a critical point that needs to be explained. In MATLAB, strictly speaking, everything is an array. To verify that this is so, let's declare a single numeric variable. If we use the whos command, we can see the size of the variable listed as 1×1:

>> x=42;
>> whos x

  Name      Size            Bytes  Class     Attributes
  x         1x1                 8  double

So even though it appears as a scalar value, it is in fact a 1×1 array. Moreover, because MATLAB can dynamically resize arrays, we can take this x and turn it into a 1×2 array just by writing:

>> x(2) = 24

x =
      42             24

You can clearly see from the output that x has become a 1×2 array.

This brings us to the idea of cell arrays. Essentially, if you consider structures as entities whose fields are addressable via their names, then cell arrays are that same idea, but the elements are kept in an array, and are addressable by position. Let us define that same person entity we met previously, but using a cell array instead:

>> address = { 123, 'London Road' };
>> person = { 'John', 22, address }

person =
    'John'    [22]    {1x2 cell}

As you can see, instead of using named fields, we simply list the elements of a cell array inside curly braces (as opposed to round brackets which are used for ordinary arrays). Now if we do a whos person you'll see it defined with a class of cell and a size of 1x3.

>> whos person

  Name        Size            Bytes  Class    Attributes
  person      1x3               606  cell

So then, the question becomes, how do we get the person's name? You could be forgiven for thinking that the name is actually the first element of the array. Indeed if you type person(1) you get something that looks very much like a string. However, what you get back is not quite a string, as can be verified by using whos again:

>> person(1)

ans =
    'John'

>> whos ans

  Name      Size            Bytes  Class    Attributes
  ans       1x1               120  cell

What you got is a cell from the cell array. If you want to get the actual value, instead of using round parentheses, you use curly braces, so writing person{1} gives us the name and we can immediately verify that it is, in fact, a string:

>> person{1}

ans =
John

>> whos ans

  Name      Size            Bytes  Class    Attributes
  ans       1x4                 8  char

Cell arrays are, essentially, a trick to keep different types of data within an array. If you have just a single type of data (say, numeric data), it is best to just use ordinary arrays. We will take a more in-depth look at arrays in Chapter 4.

Function Handles

A function handle is a variable, but instead of storing a numeric or textual value, it actually points to a different function. If you are used to ordinary programming, you can think of a function handle as a function pointer or a delegate. For example, you can define a variable that points to the sin function and then use it for invocation.

>> s=@sin;
>> s(pi/2)

ans =
       1

One thing you can do with function handles is quickly define functions—instead of writing a full declaration (and we will get to function declarations when we discuss scripts), you simply define a handle providing its body (also known as an anonymous function).

>> sumOver3 = @(x,y) (x+y)/3

sumOver3 =
    @(x,y)(x+y)/3

In the previous definition, the parentheses that follow the @ sign contain the expected function parameters, and those parameters are subsequently used to perform the calculations. Now I can invoke this function with two parameters:

>> sumOver3(2,1)

ans =
       1

One reason to use function handles is to pass functions into other functions. This method is often used by MATLAB’s “function functions” such as minimization or optimization routines.

Let us make another function that also takes two parameters plus a function, adds 1 to every parameter, then applies the function.

>> incApply = @(x,y,f) f(x+1,y+1)

incApply =
    @(x,y,f)f(x+1,y+1)

We can now use our sumOver3 function as a parameter to invoke this new function:

>> incApply(2,1,sumOver3)

ans =
          1.66666666666667

Scroll To Top
Disclaimer
DISCLAIMER: Web reader is currently in beta. Please report any issues through our support system. PDF and Kindle format files are also available for download.

Previous

Next



You are one step away from downloading ebooks from the Succinctly® series premier collection!
A confirmation has been sent to your email address. Please check and confirm your email subscription to complete the download.