Documentation Center

  • Trial Software
  • Product Updates


Class: categorical

Plot histogram of categorical data

The nominal and ordinal array data types might be removed in a future release. To represent ordered and unordered discrete, nonnumeric data, use the MATLAB® categorical data type instead.


N = hist(...)
[N,X] = hist(...)


hist(Y) plots a histogram bar plot of the counts for each level of the categorical vector Y. If Y is an m-by-n categorical matrix, hist computes counts for each column of Y, and plots a group of n bars for each categorical level.

hist(Y,X) plots bars only for the levels specified in X. X is a categorical vector or a cell array of level names as strings.

hist(ax,...) plots into the axes with handle ax instead of gca.

N = hist(...) returns the counts for each categorical level. If Y is a matrix, hist works down the columns of Y and returns a matrix of counts with one column for each coluimn of Y and one row for each cetegorical level.

[N,X] = hist(...) returns the categorical levels corresponding to each count in N, or corresponding to each column of N if Y is a matrix.


Create a histogram of age groups from the hospital.mat dataset:

load hospital
edges = 0:10:100;
labels = strcat(num2str((0:10:90)','%d'),{'s'});
AgeGroup = ordinal(hospital.Age,labels,[],edges);
AgeGroup = droplevels(AgeGroup);

See Also

| |

Was this topic helpful?