Categorical Arrays in MATLAB
Last Updated :
26 Apr, 2025
In MATLAB, categorical is a data type which can assign values from a finite, discrete set of values. For example, consider there are three categories, 'positive', 'negative', and 'null' so, a categorical array, which is an array of categorical data type, will only take data which has values from the above three categories. So, a categorical array from the above categories could be:
% categorical array example
cat_arr = {'null', 'positive', 'negative'};
This would create a 3x1 categorical array. Now, without much delay, let us see how these arrays could be created in various ways.
We can create a categorical array from an existing array.
Example 1:
Matlab
% MATLAB code for categorical arrays
% Array describing categories of patients in a hospital
arr = {'alive', 'dead', 'coma'};
% Converting this array into a categorical array
arr = categorical(arr);
% To check the categories
categories(arr)
Output:
The third statement in the above code gives the categories present in the array arr, see the output below
 We can also add a category in the categorical array used above, which is not present in the original array. For example, say we want another category of patients 'healed' but, no such patient is currently present in hospital so, we can do that by creating an array of independent categories and then pass it to the categorical() function.
Example 2:
Matlab
% MATLAB code for categorical arrays
% categories
valueset = {'healed', 'dead', 'coma', 'alive'};
% Array describing categories of patients in a hospital
arr = {'alive', 'dead', 'coma'};
% Converting this array into a categorical
% array with valueset categories
arr = categorical(arr, valueset);
% To check the categories
categories(arr)
Output:
 As we can see, the array is the same but, the number of categories is now 4, including the 'healed' category.
Let us see how to create empty categorical arrays.
Example 3:
Matlab
% Create array of NaNs in MATLAB
arr = NaN(1,3);
% Convert to categorical
arr = categorical(arr);
% Define categories
cats=["dead", "coma", "alive"];
% Add categories using addcats()
arr=addcats(arr,cats);
% Print categories
categories(arr)
Output:
This would first convert an array of NaNs to a categorical array and then, using the addcats() function, it will add the cats categories to the array arr. The output is:
 Assigning Categorical Values To arr:
Now, let us see how to assign values of to the created empty categorical array.
Example 4:
Matlab
% Assigning Categorical Values To arr in MATLAB
% Create array of NaNs
arr = NaN(1,3);
% Convert to categorical
arr = categorical(arr);
% Define categories
cats=["dead", "coma", "alive"];
% Add categories using addcats()
arr=addcats(arr,cats);
% Assigning values
arr(1)="dead";
arr(2)="alive";
arr(3)="dead";
% Displaying the array
disp(arr)
Output:
 Note: The elements of a categorical array can be accessed the same way as of an ordinary array, using the smooth parenthesis () method.
Conclusion:
This article discussed various ways of creating categorical arrays, accessing its elements, and assigning values to the same.
Similar Reads
Cell Arrays in MATLAB
In MATLAB, cell arrays are a type of arrays which stores elements of different data types and sizes in different cells, or one could say that cell arrays allow users to store heterogeneous data into a single array. For example, let us create a cell array which holds the name and age of a person. Exa
2 min read
Factorial in MATLAB
MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. In this article, we'll be calculating the factorial of a number n using MATLAB's built-in function 'factorial(number)'. Factorial:The
1 min read
Create Array of Zeros in MATLAB
MATLAB generally stores its variables in matrix forms, also in array and vector form. Sometimes, we often need a matrix(or array or vector) of zero(s) for some specific operations. We can create a matrix of zero(s) manually or with the help of the in-built function of MATLAB. The in-built function t
5 min read
VBA Arrays in Excel
Arrays are used to store data of similar data types. Suppose there are 300 students rather than declaring 300 variables for students, we can declare one array where we can store 300 elements. In this article, we will learn about excel VBA arrays. Declaration of Array Declaring an array is similar to
5 min read
2D Array Interpolation in MATLAB
In this article, we are going to discuss "2D Array Interpolation" in MATLAB with the help of two linspace() and interp2() functions. Functions Usedlinspace( ) The linspace() function is used for the generation linearly spaced vector. Syntax: linspace(a, b) linspace(a, b, n) Here, linspace(a, b) is
4 min read
Importing Data in MATLAB
MATLAB provides a simple way of importing data into a script using the importdata() function. This function takes various inputs and can be used in following forms. Method 1:This simply takes the data file and creates a suitable struct for the same within the script. Example 1: Matlab % MATLAB Code
2 min read
MATLAB Find Exact String in Cell Array
Cell arrays in MATLAB store data of various data types as a cell. These cells could contain data of different types but belong to the same array. Now, this article is focused on finding an exact string in a cell array in MATLAB. This can be done easily by using a combination of two MATLAB functions,
2 min read
MATLAB Find Closest Value in Array
MATLAB arrays store numbers and allow users to perform various operations on them. In this article, we shall see how to find the closest value to a given target value in a MATLAB array. The same task can be performed by using the nearest neighbor interpolation, which we shall explore in the followin
2 min read
Anonymous Functions in MATLAB
A block of code that is organized in such a way that is reusable for the entire program. Functions are used for reducing efforts made by writing code and making the program short, and easily understandable. There are different syntaxes for declaring a function in different programming languages. In
5 min read
Print summary of table, timetable, or categorical array in MATLAB
MATLAB provides various ways to store and organize data such as tables, categorical arrays, arrays, timetables, vectors, etc. MATLAB deals with big data used for machine learning and other mathematical functions which require different organization of data. For small data it is efficient to take a l
6 min read