Open In App

Print summary of table, timetable, or categorical array in MATLAB

Last Updated : 25 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 look at the entire data and summarize it but, the same cannot be done when you are working with big data or binary data(.mat files).

For such situations, MATLAB provides the summary function which returns a brief summary of the data structure passed to it. It must be noted here that the summary function works only on three type of MATLAB objects:

  1. Table
  2. Timetable
  3. Categorical Array/Vector

In this article, we shall see how to use the summary function on the above-mentioned MATLAB objects.

Print summary of a table, timetable, or categorical array in MATLAB

The syntax of finding summary in MATLAB for any data that can be represented in a tabular form or categorical array or a datetime frame is given by:

s = summary(<object-name>, <optional-argument>)

Here, the object name could be either a table, timetable, or categorical array and the optional argument takes the dimension along which you need a summary. The dimension argument is optional and without it, summary returns the summary along all dimensions.

Summary of a Table

Summary function does not take any optional argument for tables or timetables therefore, we always get the summary of entire tables. Let us see the same with the help of examples.

First, we will create a table from random vectors and then will print out its summary using the summary() function.

Matlab




% Variables
emp_id = [12; 34; 37; 45; 65];
emp_names = {'Harry'; 'Sean'; 'Mark'; 'Maria'; 'Mia'};
salaries = [1500; 5400; 2300; 3000; 9000];
 
% creating table with above variables
rec = table(emp_id,emp_names,salaries);


Output:

>> rec

rec =

  5x3 table

    emp_id      emp_names        salaries
    ______    _________    ________

      12                  {'Harry'}              1500  
      34                 {'Sean' }              5400  
      37                 {'Mark' }              2300  
      45                {'Maria'}              3000  
      65                 {'Mia' }                9000  

We have created three column vectors above and using them, we created a table named rec. This table would look like this.

Now, we will print the summary of this table by typing the following command in the command space.

Matlab




% printing summary
summary(rec)


Output:

>> summary(rec)

Variables:

    emp_id: 5x1 double

        Values:

            Min          12   
            Median       37   
            Max          65   

    emp_names: 5x1 cell array of character vectors

    salaries: 5x1 double

        Values:

            Min         1500  
            Median      3000  
            Max         9000  

Here, we can see that for the columns that contain numeric data, summary returns statistical values such as minimum, median, and maximum value whereas for the column that contains only text/character data, it simply return the size of the vector.

Summary of Time Tables

Summary function works in the same manner on time tables, as it works on ordinary tables. We pass the timetable object as an input and receive the summary.

In this example, we shall first create a timetable and then shall call the summary function on it.

Matlab




% variables for time table
dates = datetime({'2023-04-20'; '2023-04-21'; '2023-04-22'});
temp = [20; 23; 17];
 
% creating timetable
weather = timetable(dates,temp);


Output:

>> weather

weather =

  3x1 timetable

              dates              temp
    ___________    ____

    20-Apr-2023             20 
    21-Apr-2023              23 
    22-Apr-2023              17 

Here, we have collected a data of temperatures on three dates. Then we created a timetable named weather. Now, we shall call the summary command in the command window to receive the summary of this timetable.

Matlab




% Code
summary(weather)


Output:

>> summary(weather)

RowTimes:

    dates: 3x1 datetime
        Values:
            Min           20-Apr-2023 
            Median        21-Apr-2023 
            Max           22-Apr-2023 
            TimeStep      24:00:00    

Variables:

    temp: 3x1 double

        Values:

            Min          17   
            Median       20   
            Max          23   

Here we get similar output as of an ordinary table. Since the first column contains datetime data, we get the minimum, maximum and the median value along with a timestamp. For the second column, we receive summary containing the maximum, minimum and median value of the temperatures.

Summary of Categorical Arrays

We can get the summary of categorical array by simply passing it to the summary command. Here, we get two scenarios. First, when the array is a 1D vector and second when it is a 2D array. We shall see the same with the help of examples.

1D Categorical Arrays

When we call the summary function on a categorical array, it returns the category count of that array. See the following example.

Matlab




% creating a categorical array
cat = categorical({'red' 'bus' 'ship' 'bus' 'bike' 'car' 'red'});
% calling the summary function
summary(cat)


Output:

>> summary(cat)
     bike      bus      car      red      ship 
     1              2          1           2          1    

Firstly, we have created a categorical array and then, we called the summary function on it. The output will give us the category count along with the names of the categories.

2D Categorical Arrays

When we call the summary function on a 2D categorical array, it returns the category count along a particular dimension. The default dimension value is 1 i.e., column-wise. Let’s see how this work.

Matlab




% 2D categorical array
cat = categorical({ 'red' 'bus';
                    'ship' 'bus';
                    'bike' 'car';
                    'red' 'ship'});
% calling the summary function
summary(cat)


Output:

>> summary(cat)
     bike      1      0 
     bus       0      2 
     car       0      1 
     red       2      0 
     ship      1      1

Here, we have created a 2D categorical array with 5 categories. When we call the summary function on this array, it will display the column-wise category count, which is the default option.

Now, we can also retrieve the summary row-wise or along dimension 2. For doing so, we only need to pass the dimension value as 2. See the following code:

Matlab




% 2D categorical array
cat = categorical({ 'red' 'bus'; ...
                    'ship' 'bus'; ...
                    'bike' 'car'; ...
                    'red' 'ship'});
% rowwise summary
summary(cat,2)


Output:

>> summary(cat,2)
     bike      bus      car      red      ship 
     0         1        0        1        0    
     0         1        0        0        1    
     1         0        1        0        0    
     0         0        0        1        1    

This code will print the summary (in this case the category count) row-wise. We can clearly verify that all the category counts are given row-wise.

Conclusion

In this article, we explained how to print summary of a table, timetable, or categorical array/vector in MATLAB using the summary function. We explained summary of each object with the help of examples and also described various options available with the summary function.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads