Lesson 2.5

Data Types


  • Understand the programming concept of a data type.
  • Check the data type of variables.
  • Convert between data types.

What Is a Data Type?

In computer programming, a data type is classification (or type) of the data you use to program. That is, MATLAB categorizes each variable or any piece of data into different groups which can be used for different things. For example, MATLAB will treat a numeric integer (1, 5, 99, etc.) differently than an alphabetical character (“a”, “U”, etc.). Recall in the previous Inputs and Outputs lesson (LINK TO LESSON), we had to tell the fprintf() function what data type we were giving it (e.g., %s for string).

MATLAB refers to the different data types as fundamental MATLAB classes: MATLAB uses these terms interchangeably. For someone familiar with object oriented programming (C, C#, Java, etc.), this may be a little confusing. However, for the scope of this lesson, do not overthink this concept.

MATLAB Data Types

While the range of data types a programming language supports varies with the language, MATLAB solely works with six fundamental data types. Fundamental MATLAB data types are:

  • Numeric: 1, 2, -54, 4.56
  • Logical: True, False
  • Character: “g”, “M”, “.”, “ ”
  • Cell: Can contain any data type
  • Table: Data stored in tabular format
  • Struct: Groups related data using data containers called fields

While there are multiple data types available to MATLAB users, this course will focus on the most common such as numeric, logical, or character/strings.

Why Are Data Types Important?

The types of data (and therefore the “data types”) you will use in MATLAB will vary depending on their function and application. Some MATLAB functions only accept a specific data type, while other functions accept multiple forms of data. For example, MATLAB function fprintf() requires a character data type input (fprintf(‘hey there!’)), while the mean() function requires numeric data types, mean([1.0, 2.5, 2.0, 3.0, 6.6]).

How to Check the Data Type of a Variable

Let’s say you want to know what the data type of a variable is. For example, you would like to probe the variable myVariable and find out its data type. This can be easily achieved using the class() MATLAB function. Note that in the following example, the output is double. double is a sub data type of a numeric.

Example 1: Determine the Data Type of a Variable
myVariable = 5.678; %Defining a variable class(myVariable) %Class function to find the data type of the variable Command Window Output
ans = double [Try this code yourself with Octave Online! Click Here]

You can also examine a particular variable for some specific data type. That is, you can ask MATLAB if the particular variable is a specific data type, where MATLAB will respond with a either yes, true (1), or no, false (0). This is achieved using a variety of MATLAB data type identification functions.

One of these functions, ischar(), is used if you would like to see if a particular variable is a character data type. In the following example, a numeric double input of 5.678 into the ischar() function returns false, which means that 5.678 is not a character. On the other hand, notice that isnumeric() will return a true.

Example 2: Determining if a Variable is a Specified Data Type
myVariable = 5.678; %Variable definition ischar(myVariable) %Asks if the variable is a ‘character’ data type isnumeric(myVariable) %Asks if the variable is a ‘numeric’ data type Command Window Output
ans = logical 0 ans = logical 1 [Try this code yourself with Octave Online! Click Here]

Converting Between Data Types

MATLAB provides functions to convert between most data types. Many of these have straightforward names like num2str(), which converts a number to a string, or character array. Others are simply the name of the MATLAB class like char() or double(). We will cover more of these functions as appropriate in the following lessons. See MATLAB documentation for a full list of data type conversion functions.

Example 3: Converting Between Data Types
myVariable = 5.678; %Defining a variable class(myVariable) %Displaying the data type of the variable myVariable2 = num2str(myVariable) %Converting the variable from numeric to string data type class(myVariable2) %Displaying the data type of the new converted variable Command Window Output
ans = double myVariable2 = 5.678 ans = char [Try this code yourself with Octave Online! Click Here]

Next Lesson

In the next lesson, we will learn how to define sets of numbers, called vectors or matrices, in MATLAB.