MATLAB: Get closest x value to y value from a cumulative histogram

cumulative histogramextract datahistogram

Hi, This seems like a really simple question but I could not find any answers anywhere!
I have cumulative histogram of data and I want to find the x value that 95% of total values are above, and the x value that 5% of total values are above. This corresponds to 0.05 and 0.95 on the y axis. I have a very simple code simply using the histogram function.
histogram(i,100,'Normalization','cdf');
and I attached an image of the resulting histogram. I know I can use the data curser but I have 22 histograms I need to do it for so was hoping for a more automated way….
Help greatly appreciated!

Best Answer

If you have access to a toolbox with the prctile function (I don't), that will probably do the job.
Alternatively, try this:
h = histogram(i,100,'Normalization','cdf');
Values = h.Values;
BinCenters = (h.BinEdges(1:end-1)+h.BinEdges(2:end))/2;
% Eliminate any flat parts of the CDF:
FlatValues = find(diff([h.Values])==0)+1;
Values(FlatValues) = [];
BinCenters(FlatValues) = [];
percentiles = interp1(Values,BinCenters,[0.05,0.95])