Output of histfit without the plot

Is there a way to get the fitted curve generated by histfit without it being plotted? I want the curve it generates, but I don't want the plot it generates. Thanks times a million in advance.
Bill

 Accepted Answer

Try this:
r = normrnd(10,1,100,1); % Create Data
h = histfit(r,6);
set(gcf,'Visible','off')
CurveX = h(2).XData;
CurveY = h(2).YData;
.

8 Comments

I thought about doing something like that, but I would prefer to not have to have any graphics generated. I came across fitdist, so I ended up doing something like the following:
x = (-6:0.1:6);
r = randn(1024,1);
pd = fitdist(r, 'Normal');
pdr = pdf(pd, x);
% See the fit
figure
histogram(r,'Normalization','pdf');
hold on; grid on;
plot(x,pdr,'LineWidth',2)
Thanks for your response!
My pleasure!
In my code, none of the graphics are visible. A slightly different version is:
r = normrnd(10,1,100,1); % Create Data
figure
set(gcf,'Visible','off')
h = histfit(r,6);
CurveX = h(2).XData;
CurveY = h(2).YData;
I thought of using fitdist and pdf, however since histfit is what you specified, does everything in one call, and you didn’t mention wanting the parameters, I decided to go with that.
I did not know about fitdist when I asked my question. I came across that later, but before I saw your first response. As it turns out, I did want to plot the fit, so using histfit and grabbing the data like you show above would've done what I wanted!! Anyway, I now know two solutions, and there's nothing wrong with that!!
Thanks again!!
As always, my pleasure!
Hi,
I am wondeirng if there is a way to only plot the fit but not the histogram, using histfit?
Thanks
@Charlotte Han — There is.
Two options —
figure
hhf = histfit(2*randn(1,100)+1, 25);
Bars = findobj(hhf, 'Type','Bar');
delete(Bars) % Delete 'Bar' Plot
Lines = findobj(hhf, 'Type','Line');
figure
plot(Lines.XData, Lines.YData, '-r', 'LineWidth',2) % Save & Plot 'Line'
.
Much appreciated!
Am I able to have two fits on the same plot but not on top of each other, but side by side like subplots?
I am not certain what you want to do. See if the tiledlayout function will work.
A Vote would be appreciated!
.

Sign in to comment.

More Answers (1)

Babar
Babar on 27 May 2022
r = normrnd(10,1,100,1); % Create Data
h = histfit(r,6);
set(gcf,'Visible','off')
CurveX = h(2).XData;
CurveY = h(2).YData;

Products

Tags

Asked:

on 26 Aug 2020

Commented:

on 3 Mar 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!