Adding several plots to one specific (not current) figure

The code below is simplified. But I struggle to find out how I can update a specific figure (maybe by the specific name?).
I want to add some x,y plots to one specific figure.
for p=1:length(vector)
%above this pointthere are several plots and many figures made both inside this for loop and outside
figure('Name',figurnavn1)
hold on
plot(x,y)
end
end
%below this point there are several plots and many figures made both inside this for loop and outside

 Accepted Answer

Get the handle when you create that one special figure. Then pass it to figure()
hSpecial = figure('Name', 'My Special Figure');
Then in the for loop:
figure(hSpecial); % Switch to this specific figure.

5 Comments

Thanks for your input, but I could not see that this solves my issue adding new plot to the figure .
Then give us your entire script and let us know which is the one that you always want to switch to at the bottom of the for loop.
I believed when i entered the figure(hSpecial) below I selected the figure I want to update, and that the hold on remembered that this specific figure should have added a plot when the for loop iterates. But it seems like when I am creating other charts above and below the hSpecial figure part in script below it "forgets" to hold on to the hSpecial...
hope this makes things a bit more clear.
figure(hSpecial)
plot(prosent,varighet_yakse_fordeling)
hold on
There are two for loops outside the script below as well..indicated below giving some of the variables required in the script attached.
for ....
for....
script below
end
end
dummy_matrise=Data.Data;
dummy_matrise(dummy_matrise==0)=NaN;
%CHARTS , standard tidsplot,aspen tag
%************************************************************
if Standard_tidsplot=="Yes"||(Standard_tidsplot=="YES")||(Standard_tidsplot=="yes")
s0=chart_titles(i);
s0=char(s0);
s1=num2str(aarstall(itime));
s2=num2str(i);
figurnavn=strcat(s0,{' '},s1,' nr ',s2);
figurnavn=char(figurnavn);
figure('Name',figurnavn)
plot(tid_dato_ufiltrert,Data.Data(:,i))
xlabel('Time')
ylabel(chart_y_axis_names(i))
tittel_1=chart_titles(i);
tittel_2=strcat('Tag:',{' '},TagNames(i));
title(tittel_1,tittel_2)
grid on
if ((chart_free_text(i)=="NA")||(chart_free_text(i)=="Na")||(chart_free_text(i)=="nA"))
s30=[];
else
s30=chart_free_text(i);
end
s20=strcat("Treff filterkriteriet = ",num2str(stoppepunkter(itime,i)));
txt=[s20;s30];
textbp(txt)
saveas(gcf,figurnavn,'png');
if (utvikling_over_tid=="Yes")||(utvikling_over_tid=="YES")||(utvikling_over_tid=="yes")
% legg til alle plots i samme graf
%må da ha "relativ tid" på x akse og at alle tidsintervall er
%spesifert med likt antall dager/tid
else %gjør ingenting
end
else %gjør ingenting
end
%************************************************************
%evt beregnede tags
if ((beregning_aspen_data(i)=="NA")||(beregning_aspen_data(i)=="Na")||(beregning_aspen_data(i)=="nA"))
%gjør ingenting
else
str=strcat('@(x)',beregning_aspen_data(i));
fh = str2func(str);
beregning=fh(Data.Data);
s0=chart_titles_beregning(i);
s0=char(s0);
figurnavn=strcat(s0,{' '},s1,' nr ',s2);
figurnavn=char(figurnavn);
figure('Name',figurnavn)
plot(tid_dato_ufiltrert,beregning)
xlabel('Time')
ylabel(chart_y_axis_names_beregning(i))
tittel_2=strcat('Tag:',{' '},TagNames(i));
title(s0,tittel_2)
if ((chart_free_text_beregning(i)=="NA")||(chart_free_text_beregning(i)=="Na")||(chart_free_text_beregning(i)=="nA"))
s30=[];
else
s30=chart_free_text_beregning(i);
end
s20=strcat("Treff filterkriteriet = ",num2str(stoppepunkter(itime,i)));
txt=[s20;s30];
textbp(txt)
grid on
saveas(gcf,figurnavn,'png');
end
%************************************************************************************************
% %Varighetsdiagram std aspen tag
if Varighetsdiagram=="Yes"||(Varighetsdiagram=="YES")||(Varighetsdiagram=="yes")
[prosent,varighet_yakse_fordeling]=varighetsdiagram(tid_dato_ufiltrert,Data.Data(:,i));
verdi_median=median(dummy_matrise(:,i),'omitnan');
s0=chart_titles(i);
s0=char(s0);
s1=num2str(aarstall(itime));
s2=num2str(i);
figurnavn=strcat('Varighetsdiagram',{' '},s0,{' '},s1,' nr ',s2);
figurnavn=char(figurnavn);
figure('Name',figurnavn)
plot(prosent,varighet_yakse_fordeling)
xlabel('% of time interval specified')
ylabel(chart_y_axis_names(i))
tittel_1=chart_titles(i);
tittel_2=TagNames(i);
tittel_linje1=strcat('Varighetsdiagram',{' '},s0);
tittel_linje2=strcat('Data for tidsperiode',{' '},char(tid_dato_ufiltrert(1)),{' '},'til',{' '},char(tid_dato_ufiltrert(end)),{', '},'Tag:',{' '},tittel_2);
title(tittel_linje1,tittel_linje2)
% tekstbox
%ypos=(max(Data.Data(:,i))-min(Data.Data(:,i)))/10+min(Data.Data(:,i)); %plasserer 1/5 ned på ift differanse mellom maks og min verdi på y vektor
%xpos=2.5;
txt={'The process variable is at x % greater than ','chart Y axis for the specified time periode'};
%text(xpos,ypos,txt) % 5% x axis
s10=strcat("Average=",num2str(round(mean(dummy_matrise(:,i),'omitnan'),2)),", ","Median=",num2str(round(median(dummy_matrise(:,i),'omitnan'),2)),", Max=",num2str(round(max(dummy_matrise(:,i)),2)));
%text(xpos,0.95*max(Data.Data(:,i)),s10);
if ((chart_free_text(i)=="NA")||(chart_free_text(i)=="Na")||(chart_free_text(i)=="nA"))
s30=[];
else
s30=chart_free_text(i);
end
s20=strcat("Treff filterkriteriet = ",num2str(stoppepunkter(itime,i)));
s40="The process variable is at x % greater than";"chart Y axis for the specified time periode";
s50="chart Y axis for the specified time periode";
%txt=[s20;s10;s30];
txt=[s20;s10;s30;s40;s50];
%textbp(txt)
text(1,(max(dummy_matrise(:,i))-min(dummy_matrise(:,i)))/2,txt);
grid on
saveas(gcf,figurnavn,'png');
if (utvikling_over_tid=="Yes")||(utvikling_over_tid=="YES")||(utvikling_over_tid=="yes")
% JA til legg til alle plots i samme graf
for p=1:length(tagnr_utvikling_over_tid)
if tagnr_utvikling_over_tid{p,1}==i
% dette er taget som skal puttes i samme graf for de
% ulike tidsintervall
% Data.tagnr_utvikling_over_tid=varighet_yakse_fordeling;
figurnavn=strcat('Varighetsdiagram samlet',{' '},tittel_2);
figurnavn=char(figurnavn);
hSpecial=figure('Name',figurnavn,'NumberTitle','off');
%set(0,'CurrentFigure',hSpecial);
figure(hSpecial)
plot(prosent,varighet_yakse_fordeling)
hold on
else%gjør ingenting
end
end
% plot(prosent,varighet_yakse_fordeling)
% hold on
%plot
%hold on
%plot
%AxesH = axes('NextPlot', 'add')
%plot(x, y, 'Parent', AxesH)
else %gjør ingenting
end
else %gjør ingenting
end
%************************************************************
%Varighetsdiagram evt beregnede tags
if ((beregning_aspen_data(i)=="NA")||(beregning_aspen_data(i)=="Na")||(beregning_aspen_data(i)=="nA"))
%gjør ingenting
else
str=strcat('@(x)',beregning_aspen_data(i));
fh = str2func(str);
beregning=fh(Data.Data);
[prosent,varighet_yakse_fordeling]=varighetsdiagram(tid_dato_ufiltrert,beregning);
s0=chart_titles_beregning(i);
s0=char(s0);
figurnavn=strcat('Varighetsdiagram',{' '},s0,{' '},s1,' nr ',s2);
figurnavn=char(figurnavn);
figure('Name',figurnavn)
plot(prosent,varighet_yakse_fordeling)
xlabel('% of time interval specified')
ylabel(chart_y_axis_names_beregning(i))
%tittel_1=chart_titles(i);
tittel_2=TagNames(i);
tittel_linje1=strcat('Varighetsdiagram',{' '},s0);
tittel_linje2=strcat('Data for tidsperiode',{' '},char(tid_dato_ufiltrert(1)),{' '},'til',{' '},char(tid_dato_ufiltrert(end)),{', '},'Tag:',{' '},tittel_2);
title(s0,tittel_linje2)
beregning(beregning==0)=NaN;
verdi_median=median(beregning,'omitnan');
% tekstbox
%ypos=(max(beregning)-min(beregning))/10+min(beregning); %plasserer 1/5 ned på ift differanse mellom maks og min verdi på y vektor
%xpos=2.5;
txt={'The process variable is at x % greater than ','chart Y axis for the specified time periode'};
%text(xpos,ypos,txt) % 5% x axis
s10=strcat("Average=",num2str(round(mean(beregning,'omitnan'),2)),", ","Median=",num2str(round(median(beregning,'omitnan'),2)),", Max=",num2str(round(max(beregning),2)));
%text(xpos,0.95*max(beregning),s10);
% if ((chart_free_text_beregning(i)=="NA")||(chart_free_text_beregning(i)=="Na")||(chart_free_text_beregning(i)=="nA"))
% s30=[];
% else
% s30=chart_free_text_beregning(i);
% end
% s20=strcat("Treff filterkriteriet = ",num2str(stoppepunkter(itime,i)));
% txt=[s20;s10;"The process variable is at x % greater than";"chart Y axis for the specified time periode";s30];
% textbp(txt)
grid on
saveas(gcf,figurnavn,'png');
if (utvikling_over_tid=="Yes")||(utvikling_over_tid=="YES")||(utvikling_over_tid=="yes")
% legg til alle plots i samme graf
else %gjør ingenting
end
end
%************************************************************************************************
You didn't do what I said. You did
hSpecial=figure('Name',figurnavn,'NumberTitle','off');
%set(0,'CurrentFigure',hSpecial);
figure(hSpecial)
plot(prosent,varighet_yakse_fordeling)
OK, let's look what this does. First you call figure() and give it a certain name and get the handle to the figure back in hSpecial. This will create a new figure window.
Then you call figure(hSpecial) which switches to that figure, but since you had just created it, there was no need to switch to it -- it already was the current figure so no switching was needed. And then you plot to it.
But, unless I missed something, that's the only time you deal with hSpecial. You had said that you were creating and plotting to other figures and then wanted to switch back to hSpecial, like
hSpecial = figure('Name', figurnavn, 'NumberTitle', 'off');
plot(prosent,varighet_yakse_fordeling)
h1 = figure('Name', 'First figure'); % Create a new figure.
plot(1:10);
h2 = figure('Name', 'Second figure'); % Create a new figure.
plot(2:33);
% Now switch back to the already-created special figure.
figure(hSpecial); % Pass it the HANDLE, not a name.
% Plot to the special figure.
plot(3:44);
So the above code will let you plot to other figures but then when you want, you can switch back to the special figure and plot to it.
Thanks for your help. It works now. I made array with figures handles, and restricted the handle to only be generated one time for each different tag I analyse (outside for loop). Then switch to the figure handle as you say works fine. Thanks again !
for p=1:length(tagnr_utvikling_over_tid)
if (tagnr_utvikling_over_tid{p,1}==i)
% dette er taget som skal puttes i samme graf for de
% ulike tidsintervall
tittel_2=TagNames(i);
figurnavn=strcat('Varighetsdiagram samlet',{' '},tittel_2);
figurnavn=char(figurnavn);
if itime==1
hSpecial_vektor(p)=figure('Name',figurnavn,'NumberTitle','off'); I ne
else
end
figure(hSpecial_vektor(p))
plot(prosent,varighet_yakse_fordeling)
hold on
else%gjør ingenting
end
end

Sign in to comment.

More Answers (2)

Hmm...I could not get this to work. the code below is within other loops. The problem is that even though I have "hold on" when the external loop iterates I want to add a new plot to the figure(hSpecial). The figurename is changing as a result of the outer for loop. But each time the figure(hSpecial) creates a new figure window even if the figurename is equal to the last one it creates a new window instead of updating the chart with a new plot that I want.
So when the external loop is on loop nr 2 , and the if cricteria is met I want to update those charts with new x,y data. Not creating a new chart.
for p=1:length(tagnr_utvikling_over_tid)
if tagnr_utvikling_over_tid{p,1}==i
% dette er taget som skal puttes i samme graf for de
% ulike tidsintervall
% Data.tagnr_utvikling_over_tid=varighet_yakse_fordeling;
figurnavn=strcat('Varighetsdiagram samlet',{' '},tittel_2);
figurnavn=char(figurnavn);
hSpecial=figure('Name',figurnavn,'NumberTitle','off');
%set(0,'CurrentFigure',hSpecial);
figure(hSpecial)
plot(prosent,varighet_yakse_fordeling)
hold on
else%gjør ingenting
end
end

2 Comments

hello again
wonder if there is another way to solve your problem
maybe it's a bit the hammer solution but why not put your data in a structure (or simply an array if that can suffice) and each time you add new data to the previous set , simply plot again the whole thing
sure not the most elegant manner but...
yes, that would work. However, I would say that is my backup scenario. I would prefer to avoid doing that. Currently I am overwriting many variables (large data set) in each iteration. If storing each variable to the end with very large matrices that generates more memory requirement. Not ideal. So hopefully I will get this to work inside the for loop.
But I am quite a newbie in programming so there is probably much better ways to organize my code than what I have done...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!