2

Im creating histograms using two scripts, one is matlabs own hist function and another is a script I downloaded. The script I downloaded takes the absolute min and max values and generates a histogram between that. But the issue is that unlike MATLAB, this histogram is not displayed. I am presented with a vector.

Now to compare the two visually I am using plot, but for some reason the scale changes. For example histogram using MATLAB's hist is shown below:

enter image description here

And if I show this histogram in plot, the x-axis scale changes:

enter image description here

How can I keep the scale same?

I need this because the downloaded script does not generate the histogram so to display it I use plot. Again the plot is between 0 and 100 and I feel this may not be an accurate comparison

StuckInPhDNoMore
  • 2,507
  • 4
  • 41
  • 73
  • 2
    Which version of MATLAB is this? Normally you would use one of the outputs of `hist` (bin centers) as the `x` values for the 2nd plot,instead of just plotting using `plot(counts)`... – Dev-iL Mar 07 '17 at 16:16
  • I'm using Matlab 2016a – StuckInPhDNoMore Mar 07 '17 at 16:24
  • The script should tell you the bin edges, or better yet the bin centers it is using to build the histogram. Does it do that? – Luis Mendo Mar 07 '17 at 16:35
  • @LuisMendo Ive added the code to my question, no it does not. I'm happy with the output it generates but I also need for it to display like matlab does – StuckInPhDNoMore Mar 07 '17 at 16:40

2 Answers2

4

It seems that you're not using all the available information at your disposal. Please see the code below for an example of how what you want can be done:

%% Generate some data:
rng(42653042);
data = randn(300); data = (data-min(data(:)))*90+100;
data(1:4:end) = data(1:4:end)/2;
%% Plot using hist:
figure(); hist(data(:),100);
%% Return bin info using hist:
[N,X] = hist(data(:),100);
%% Plot the other function's output w/o X:
figure(); plot(N);
%% Plot the other function's output w/ X:
figure(); plot(X,N);
figure(); bar(X,N);

The function hist should be replaced in newer versions of MATLAB by:

  • histogram, when used for plotting (i.e. the case of hist without outputs).
  • histcounts, when used for counting (i.e. the case of hist with outputs).
Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • Thank you, I was not aware of the second output. This fixes the matlab output. But I cant compare again, as the script I am using (added above) does not provide such a second output, and when that is plotted I again get a plot between 0 and 100. – StuckInPhDNoMore Mar 07 '17 at 16:43
  • @StuckInPhD You don't need the 2nd script to output anything else. It only gives you the counts (`N`)... and the positions (`X`) you should already have from your own `hist`... – Dev-iL Mar 07 '17 at 16:44
  • Thanks @Dev-il. I need the second script to generate a histogram between a fixed minimum and maximum value, Matlab doies not have this and every histogram is adapted to its own min and max. The shape and output of the two are very different as well. For classification purposes all histograms must be between a fixed scale, say between -50 and 250. I cannot find this setting for matlab's ``hist`` – StuckInPhDNoMore Mar 07 '17 at 16:55
3

Use "n = hist(Y,x) where x is a vector, returns the distribution of Y among length(x) bins with centers specified by x", to specify the bins centers.