There are histogram estimators in Matlab's "histogram" function, which do some automatic selection of bins (this is the recommended replacement for hist/histc): https://www.mathworks.com/help/matlab/creating_plots/replace-discouraged-instances-of-hist-and-histc.html
You should probably use these rather than rolling out your own histogram estimator.
Alternatively, if you just search for "Histogram bin selection" or something similar", you'll find papers you can follow like Wand, M. P. "Data-based choice of histogram bin width." The American Statistician 51.1 (1997): 59-64.
Most of these will try to be good with respect to mean integrated square error (i.e. the integrated square difference of the histogram pdf and the true pdf). Whether or not this matters rather than the usual (range of data)/(1+log_2 n) rule in practice being good enoguh is another matter.
You can also look in Wasserman's All of Statistics chapter on nonparametric curve estimation for another treatment of selection of bin widths.