How to display multiple histogram with one run

I am generating random numbers (data) with the 3 parameter weibull distribution.
In my code, I have 25 combinations of my parameters (b=1:5; T=1:5) and I want to display every single combination as a histogram.
So at the end, I have 25 histogram of data.
The problem is, only the last combination of my parameter ( b=5, T=5) is displayed.
How can I see ALL 25 histograms with one run?
Edit: I deleted the 2 parameter weibull in my code for better overview about my question.
clear all;
n = 100;
t0 = 0.5;
b = 1:5;
T = 1:5;
for v_T = 1:length(T)
for v_b = 1:length(b)
data(:,v_b,v_T) = wblrnd(v_T,v_b, [n,1]) + t0;
end
end
histogram (data)
ax = gca;
get(ax, 'position')
ax.XLim=[0,10];

 Accepted Answer

Hello Mustafa, you can use subplots inside for loops.
clear all;
n = 100;
t0 = 0.5;
b = 1:5;
T = 1:5;
clf
figure(1)
loopVar = 0;
for v_T = 1:length(T)
for v_b = 1:length(b)
data(:,v_b,v_T) = wblrnd(v_T,v_b, [n,1]) + t0;
subplot(length(T),length(b), v_b + loopVar)
histogram (data)
ax = gca;
get(ax, 'position')
ax.XLim=[0,10];
end
loopVar = loopVar + length(T)
end

5 Comments

It works thank you!. Do you have an idea, how I can seperate them in to 5 figures. Like:
  • figure 1: T= 1 and b=1:5
  • figure 2: T= 2 and b=1:5
  • figure 3: T= 3 and b=1:5
  • figure 4: T= 4 and b=1:5
  • figure 5: T= 5 and b=1:5
Its more complicated I think...
Hello Mustafa,
Do you know how to use for loops? If not, it may be worth going through the documentation.
The key is that what you did manually (figure 1, figure 2, figure 3...) can be done programatically in a for loop. In this case, I used your v_T variable.
clear all;
n = 100;
t0 = 0.5;
b = 1:5;
T = 1:5;
for v_T = 1:length(T)
for v_b = 1:length(b)
figure(v_T) % this will count 1:5 and open a new figure each step
data(:,v_b,v_T) = wblrnd(v_T,v_b, [n,1]) + t0;
subplot(1,5,v_b)
histogram (data)
ax = gca;
get(ax, 'position')
ax.XLim=[0,10];
end
end
Thank you very much, it works. Yes I am noob in matlab... I want to compare the 2 parameter and 3 parameter distribution. So actually it should display 5 figures for data2p and 5 figures for data3p... What do I wrong...?
clear all;
n = 100;
t0 = 0.5;
b = 1:5;
T = 1:5;
for v_T = 1:length(T)
for v_b = 1:length(b)
figure(v_T) % this will count 1:5 and open a new figure each step
data2p(:,v_b,v_T) = wblrnd(v_T,v_b, [n,1]);
data3p(:,v_b,v_T) = wblrnd(v_T,v_b, [n,1]) + t0;
subplot(1,5,v_b)
histogram (data2p)
histogram (data3p)
ax = gca;
get(ax, 'position')
ax.XLim=[0,10];
end
end
Hello Mustafa,
I recommend that you take a look at Mike Cohen's courses: https://www.udemy.com/course/master-matlab-through-guided-problem-solving/
I went through them last year and taught myself MATLAB in that way. I feel that you are trying to solve problems before understanding the basics of what you are doing, which could be fixed in a few hours of practice/watching courses/reading a MATLAB book.
In this case, when you plot data3p, you are overwriting all the information of data2p. You need to plot the information in a different subplot.
clear all;
n = 100;
t0 = 0.5;
b = 1:5;
T = 1:5;
for v_T = 1:length(T)
for v_b = 1:length(b)
figure(v_T) % this will count 1:5 and open a new figure each step
data2p(:,v_b,v_T) = wblrnd(v_T,v_b, [n,1]);
data3p(:,v_b,v_T) = wblrnd(v_T,v_b, [n,1]) + t0;
subplot(2,5,v_b) % subplot is bigger, 2 rows and 5 colums
histogram (data2p)
subplot(2,5,v_b +5) % you need this new subplot here
histogram (data3p)
ax = gca;
get(ax, 'position')
ax.XLim=[0,10];
end
end
Thank you for the advice, I will five it a try!

Sign in to comment.

More Answers (1)

You did not put in
hold on

2 Comments

I dont want to bring them all in one graph, I would like to have 25 single graphs. So i can copy them out and compare them.
Well you can use subplot() or you can use the newer tiled layout operations, if you want all the histograms to appear in the same figure but diffrent axes.
Or you can add a call to figure() to create a new figure each time.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!