Foundations of computer programming in MATLAB

Data

Data is the information that computers store and process. It comes in many forms, depending on the structure and content of the information. We tend to differentiate among different types of data because information can be categorized at an abstract level and in order to operate on these different categories of data in a reasonable manner.

Primary data types

MATLAB has several different data types. Let’s consider the two most important types. We will later encounter more advanced types.

Numeric types

Numeric types are the most extensively used data type in MATLAB. Every numeric type is, by default, saved as a double, which is a double-precision floating-point number. This includes arrays, which are, by default, double-precision. (This means that each array element is a double-precision number.)

Other numeric types can be explicitly defined. These include single precision single and various-bit signed and unsigned integers such as int64.

Characters and strings

Another important data type in MATLAB is the string. These often represent characters, words, and text. The syntax to denote a string is single quotes surrounding the string. For instance, 'Hello, World!' is a string. Like all data types, these can be assigned to a variable name and manipulated with various functions.

Determining and converting data type

Given a variable x, we can determine which type of data it has been assigned by using the function whos. The syntax is whos x. This will return, among other things, the Class, or data type of the variable x.

Notice that it also returns the Size of the variable. If x is a scalar double, it will have Size 1x1, which means it is a $1 \times 1$ array. This is interesting. MATLAB considers all double data to be an array of some dimension, even if all of them are singular. This is why we often say, glibly, everything in MATLAB is an array.

It is often helpful to convert data from one type to another. There are several useful MATLAB functions for doing this, like num2str and str2num. These are especially helpful when loading data from outside MATLAB—which often arrives encoded as strings, but is usually numeric data—and when printing computational results, especially to plots.