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.
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:
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.
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]).
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.
myVariable = 5.678; %Defining a variable class(myVariable) %Class function to find the data type of the variableCommand 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.
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 typeCommand Window Output
ans = logical 0 ans = logical 1 [Try this code yourself with Octave Online! Click Here]
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.
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 variableCommand Window Output
ans = double myVariable2 = 5.678 ans = char [Try this code yourself with Octave Online! Click Here]
In the next lesson, we will learn how to define sets of numbers, called vectors or matrices, in MATLAB.