Main Content

sorty

Sort elements in heatmap column

Description

sorty(h,column) displays the elements in column in ascending order (from top to bottom). This function sorts the column elements by rearranging the rows. Specify column as one element or as an array of elements from XData. If the first column that you specify has repeating values, then sorty rearranges the remaining rows according to the second column, and so on.

example

sorty(h,column,direction), where direction is 'descend', sorts the values in descending order instead of ascending order (default). To sort in a unique direction for each column in column, specify direction as an array whose elements are 'ascend' or 'descend'.

example

sorty(___,'MissingPlacement',lcn) specifies whether to put NaN elements first or last in the sort order. Specify lcn as 'first', 'last', or 'auto'. The default setting is 'auto', which lists NaN elements last for ascending order and first for descending order. Use this option after any of the previous input argument combinations.

sorty(h) displays the elements in the first column in ascending order. If the first column has repeating elements, then sorty rearranges the remaining rows according to the second column, and so on.

C = sorty(___) returns the sorted color data, which is a matrix of values as they appear in the heatmap chart.

[C,y] = sorty(___) also returns the sorted y values in the order they are displayed.

example

Examples

collapse all

Create a heatmap of utility outages and sort the values in a particular column so that they appear in ascending order, from top to bottom.

First, create a heatmap. Read the sample file outages.csv into a table. The sample file contains data representing electric utility outages in the United States. The table contains six columns: Region, OutageTime, Loss, Customers, RestorationTime, and Cause. Create a heatmap that shows the different regions along the x-axis and the different outage causes along the y-axis. In each cell, show how many times each region experienced a power outage due to a particular cause.

T = readtable('outages.csv');
h = heatmap(T,'Region','Cause');

Figure contains an object of type heatmap. The chart of type heatmap has title Count of Cause vs. Region.

Sort the values in the 'NorthEast' column so that they appear in ascending order from top to bottom.

sorty(h,'NorthEast')

Figure contains an object of type heatmap. The chart of type heatmap has title Count of Cause vs. Region.

Create a heatmap of utility outages and display the values in the 'NorthEast' column in descending order by rearranging the rows.

T = readtable('outages.csv');
h = heatmap(T,'Region','Cause');
sorty(h,'NorthEast','descend')

Figure contains an object of type heatmap. The chart of type heatmap has title Count of Cause vs. Region.

Create a heatmap of utility outages and rearrange the rows using values from multiple columns. Sort the rows so that the column 'SouthWest' appears in ascending order. Since that column contains duplicate values, use the column 'NorthEast' to sort the remaining rows.

T = readtable('outages.csv');
h = heatmap(T,'Region','Cause');
sorty(h,{'SouthWest','NorthEast'})

Figure contains an object of type heatmap. The chart of type heatmap has title Count of Cause vs. Region.

Create a heatmap of utility outages and sort the values in the 'NorthEast' column so that they appear in ascending order, from top to bottom. Return the sorted color data and the row values (y values) in the order they appear along the y-axis.

T = readtable('outages.csv');
h = heatmap(T,'Region','Cause');
[C,y] = sorty(h,'NorthEast')

Figure contains an object of type heatmap. The chart of type heatmap has title Count of Cause vs. Region.

C = 10×5

     0     1     0     0     1
     0     5     3     0    17
     5    11     4     0     4
     9    18    42     2    85
    19    31    81     8    49
    16    41    13     3    22
    18    70    37     1    19
    32   102    54     6     7
    12   135    20     0   127
    31   143   135     6    23

y = 10x1 cell
    {'earthquake'      }
    {'fire'            }
    {'unknown'         }
    {'equipment fault' }
    {'energy emergency'}
    {'wind'            }
    {'winter storm'    }
    {'thunder storm'   }
    {'attack'          }
    {'severe storm'    }

Create a heatmap of utility outages and sort the values in the left column in ascending order (from top to bottom) by rearranging the columns. Then, restore the original order.

T = readtable('outages.csv');
h = heatmap(T,'Region','Cause');
sorty(h)

Figure contains an object of type heatmap. The chart of type heatmap has title Count of Cause vs. Region.

Restore the original row order by setting the YDisplayData property of the HeatmapChart object equal to the YData property.

h.YDisplayData = h.YData;

Figure contains an object of type heatmap. The chart of type heatmap has title Count of Cause vs. Region.

Input Arguments

collapse all

Heatmap to update, specified as a HeatmapChart object.

Column to sort, specified as a character vector, cell array of character vectors, or string array indicating of one or more elements from the XData or XDisplayData property of the HeatmapChart object.

Example: sorty(h,'MyColumnName') sorts the elements in the column called 'MyColumnName' in ascending order.

Direction to sort, specified as 'ascend', 'descend', a cell array of character vectors, or a string array.

  • 'ascend' — Sort all columns listed in column in ascending order.

  • 'descend' — Sort all columns listed in column in descending order.

  • Array — Sort in a unique direction for each column in column. Specify direction as a cell array of character vectors or a string array containing the elements 'ascend' or 'descend'. The array must be the same length as column.

Example: {'ascend','descend','ascend'} sorts the elements in the first column in ascending order. If there are any repeating values in the first column, then sorty breaks ties using the elements of the second column in descending order, and so on.

NaN placement, specified as one of these values:

  • 'auto' — List NaN elements last for ascending order and first for descending order.

  • 'first' — List NaN elements first.

  • 'last' — List NaN elements last.

Example: sortx(h,column,'MissingPlacement','last')

Output Arguments

collapse all

Sorted color data, returned as a matrix. The values in the matrix appear in the same order as they appear on the heatmap.

The ColorDisplayData property of the HeatmapChart object stores the sorted color data. The ColorData property stores the unsorted color data.

Rearranged row order, returned as a cell array of character vectors.

The YDisplayData property of the HeatmapChart object stores the sorted row order. The YData property stores the unsorted row order.

Algorithms

The sorty function sets these HeatmapChart object properties:

  • YDisplayData — Property that stores the y-axis data.

  • ColorDisplayData — Property that stores the color display data.

The sorty function also resets the YLimits property.

Version History

Introduced in R2017b

See Also

Functions

Properties