Hello All,
I am trying to draw multiple histogram in one plot using also if condition:
if(ind==1)
fig=figure;
hold on
h1=histogram(f1,'BinWidth',0.01);
xlim([1 1.4])
hold on
h2=histogram(f2,'BinWidth',0.01);
if(ind==1) % HFD.
fig=figure;
hold on
h1=histogram(f1,'BinWidth',0.01);
xlim([1 1.4])
%title('HFD(without artifacts) with treshold=', strtrsh)
%saveas(fig1,strcat(strcat('HFD(without artifacts) with treshold=', strtrsh),".fig"));
%hold on
%fig2=figure;
h2=histogram(f2,'BinWidth',0.01);
%xlim([1 1.4])
title('HFD with treshold=', strtrsh)
saveas(fig,strcat(strcat('HFD with treshold=', strtrsh), ".fig"));
elseif(ind==2) % mean.
fig=figure;
h1=histogram(f1,'BinWidth',0.025);
xlim([0 1])
hold on
%fig2=figure;
h2=histogram(f2,'BinWidth',0.025);
xlim([0 1])
title('mean with treshold=', strtrsh)
saveas(fig,strcat(strcat('mean with treshold=', strtrsh),".fig"));
elseif(ind==2)
fig=figure;
h1=histogram(f1,'BinWidth',0.025);
xlim([0 1])
hold on
h2=histogram(f2,'BinWidth',0.025);
xlim([0 1])
saveas(fig,strcat(strcat('With treshold=', strtrsh),".fig"));
saveas(fig,strcat(strcat('mean with treshold=', strtrsh),".fig"));
Unfortunately I receive for one case two different figures. From which one is saved with save command and the second one is only displayed. How to correct it to receive only one figure with two histogram? The same one should be displayed and saved?
Cheers
Elzbieta

 採用された回答

Voss
Voss 2024 年 2 月 16 日

0 投票

Here's some code that plots two histograms in one figure and saves the figure:
% some random data:
f1 = 1.2+randn(1,100)/20;
f2 = 1.2+randn(1,100)/20;
% create the figure and histograms:
fig=figure;
hold on
h1=histogram(f1,'BinWidth',0.01);
xlim([1 1.4])
h2=histogram(f2,'BinWidth',0.01);
% save the figure:
saveas(fig,'histograms.fig')
You can see that the saved figure is accurate:
openfig('histograms.fig');
It's difficult to offer more specific advice because your code is a little confusing for the following reasons:
  • The ind==2 branches will never be executed since they are inside the top-level if(ind==1) block - and ind doesn't change. And anyway, the second ind==2 branch will never be executed at all - it doesn't make sense to use the same condition more than once in an if/elseif construction.
  • The code inside the first if(ind==1) and the second if(ind==1) create nearly identical figures, with the only difference being the second figure's plot has a title. I'm not sure what that's about
Be aware that each time you do
fig=figure;
a new figure is created, and the variable fig is the new figure. If you need to refer to a previously created figure after having created a new one, then you should store the two figures as different variable names (e.g., fig1 and fig2), and if you don't mean to create a new figure then remove the fig=figure; line, which will make the subsequent plotting commands plot into the current figure.

4 件のコメント

Elzbieta
Elzbieta 2024 年 2 月 17 日
編集済み: Voss 2024 年 2 月 17 日
Thank you. In fact the figures are created in a loop including these logical conditions:
for indtrsh = 1:length(treshold)
fig = figure('Position',[250 200 1582 845]);
hold on
if(ind==1)
h1=histogram(f1,'BinWidth',0.01);
h2=histogram(f2,'BinWidth',0.01);
xlim([1 1.4])
title('HFD with treshold=', strtrsh)
savefig(fig,strcat(strcat('HFD test with treshold=', strtrsh), ".fig"));
elseif(ind==2)
h1=histogram(f1,'BinWidth',0.025);
h2=histogram(f2,'BinWidth',0.025);
xlim([0 1])
title('mean with treshold=', strtrsh)
saveas(fig,strcat(strcat('mean with treshold=', strtrsh),".fig"));
end
end
and I am still obtaining the problem that one figure is save and another displayed. They should be the same but it is not the case. What could be the reason for such behavior?
Looking forward to hearing from you!
ELzbieta
Voss
Voss 2024 年 2 月 17 日
Are ind and indtrsh supposed to be the same thing?
Is strtrsh defined as something along the lines of the following?
strtrsh = num2str(treshold(indtrsh));
If the answer to both of those questions is yes, then the code produces two figures:
% some random data:
treshold = rand(1,2);
f1 = 1+randn(1,1000)/3;
f2 = 0.8+randn(1,1000)/3;
for indtrsh = 1:length(treshold)
% providing missing variable values:
ind = indtrsh;
strtrsh = num2str(treshold(indtrsh));
fig = figure('Position',[250 200 1582 845]);
hold on
if(ind==1)
h1=histogram(f1,'BinWidth',0.01);
h2=histogram(f2,'BinWidth',0.01);
xlim([1 1.4])
title('HFD with treshold=', strtrsh)
savefig(fig,strcat(strcat('HFD test with treshold=', strtrsh), ".fig"));
elseif(ind==2)
h1=histogram(f1,'BinWidth',0.025);
h2=histogram(f2,'BinWidth',0.025);
xlim([0 1])
title('mean with treshold=', strtrsh)
saveas(fig,strcat(strcat('mean with treshold=', strtrsh),".fig"));
end
end
And the figures are each saved to file properly:
% opening the fig files:
fn = dir('*.fig');
fn = fullfile({fn.folder},{fn.name});
for ii = 1:numel(fn)
openfig(fn{ii});
end
If the problem persists, please share the complete code, with all the variables defined so I can run the code myself.
Elzbieta
Elzbieta 2024 年 2 月 23 日
移動済み: Voss 2024 年 2 月 23 日
Thank you to all of you.
Voss
Voss 2024 年 2 月 23 日
You're welcome

サインインしてコメントする。

その他の回答 (0 件)

質問済み:

2024 年 2 月 16 日

コメント済み:

2024 年 2 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by