Hello
I have two column vector, one with my ID and the other with a scalar value. For Each ID i want to compute an histogram and the probability density function. The code works, but when I save the graphs, it sum the graphs one after the other.
this is the code and then there is the workspace
load('data_3')
k = unique(id_oss);
for i = 1:numel(k)
index = (id_oss == k(i));
R1 = R(index);
[n,x] = hist(R1,10);
n = n/length(R1)/diff(x(1:2));
bar(x,n,'hist')
hold on
mu = mean(R1);
sd = std(R1);
ix = linspace(min(R1),max(R1));
iy = pdf('normal', ix, mu, sd);
plot(ix,iy);
pngFileName = sprintf('hist%d.png',i);
fullFileName = sprintf('hist%d_%d.png', i);
saveas(gcf,fullFileName);
end
Thank you for the help

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 10일
편집: Ameer Hamza 2020년 4월 10일

0 개 추천

The easiest way to solve this issue is to create a new figure in each iteration
load('data_3')
k = unique(id_oss);
for i = 1:numel(k)
figure(); % create a new figure
index = (id_oss == k(i));
R1 = R(index);
[n,x] = hist(R1,10);
n = n/length(R1)/diff(x(1:2));
bar(x,n,'hist')
hold on
mu = mean(R1);
sd = std(R1);
ix = linspace(min(R1),max(R1));
iy = pdf('normal', ix, mu, sd);
plot(ix,iy);
pngFileName = sprintf('hist%d.png',i);
fullFileName = sprintf('hist%d_.png', i);
saveas(gcf,fullFileName);
delete(gcf); % delete the figure
end

댓글 수: 2

Enrico D'Addario
Enrico D'Addario 2020년 4월 10일
thank you so much!
Ameer Hamza
Ameer Hamza 2020년 4월 10일
Glad to be of help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Histograms에 대해 자세히 알아보기

질문:

2020년 4월 10일

댓글:

2020년 4월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by