Adding several plots to one specific (not current) figure

10 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Image Analyst
Image Analyst el 31 de Ag. de 2022
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 comentarios
Image Analyst
Image Analyst el 1 de Sept. de 2022
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.
Ingvald Bårdsen
Ingvald Bårdsen el 2 de Sept. de 2022
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

Iniciar sesión para comentar.

Más respuestas (2)

Mathieu NOE
Mathieu NOE el 31 de Ag. de 2022
Movida: Image Analyst el 31 de Ag. de 2022
  1 comentario
Ingvald Bårdsen
Ingvald Bårdsen el 31 de Ag. de 2022
Thanks for your input, but I could not see that this solves my issue adding new plot to the figure .

Iniciar sesión para comentar.


Ingvald Bårdsen
Ingvald Bårdsen el 31 de Ag. de 2022
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 comentarios
Mathieu NOE
Mathieu NOE el 31 de Ag. de 2022
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...
Ingvald Bårdsen
Ingvald Bårdsen el 1 de Sept. de 2022
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...

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by