title: MATLAB Adding Gaussian fit to histogram
body: <p>I am trying to add Gaussian fit to the histogram in MATLAB, but I do not know how to apply the fit to one particular peak only. </p>
  
  <p><a href="http://postimg.org/image/phms61rdh/" rel="nofollow">http://postimg.org/image/phms61rdh/</a> first plot</p>
  
  <p><a href="http://postimg.org/image/sindrn8er/" rel="nofollow">http://postimg.org/image/sindrn8er/</a> second plot</p>
  
  <p>I am also posting part of the code I have been using: </p>
  
  <pre><code>    data_Rb = (importdata('path\name.txt'));
      counts_Rb = data_Rb.data(:,3);
      figure
      hist(counts_Rb, nbins);
      xlim([0 120]);
      title('Rubidium');
      histfit(counts_Rb,1000);
  
      pd=fitdist(counts_Rb(:),'normal')
      x=0:0.001:120;  
      PDF=pdf(pd,x); %PDF is a vector of y values: it's our fit
      PDF=PDF/max(PDF); %nor
      y=ylim;
      PDF=PDF*y(2);
      hold on
      plot(x,PDF,'r-','LineWidth',2);
      hold off
  </code></pre>
  
  <p>These two blocks give me two different Gaussians as shown in the first picture. I do not understand why they fit data so badly: is it because of the tail on the RHS? </p>
  
  <p>In the second plot I need to apply Gaussian fit to the last peak only. How should I do it?</p>
  
  <p>And finally, after applying the fit, fit results are outputed onto the screen. Is there a function to save them into an array or so for later use?</p>
  
  <p>Thanks in advance!</p>