Why do subplots created in a loop disappear on a new iteration?

11 visualizaciones (últimos 30 días)
Artem Smirnov
Artem Smirnov el 18 de En. de 2017
Comentada: Carina Schwab el 31 de En. de 2019
Hello. I am doing research for my thesis, which I have almost completed, except for one thing. I have to create a big spreadsheet (43x13) with plots. I have 42 magnetic observatories and I want their names to be written in a first column. I also want the number of the months to be written in a first row. I guess, I should do that like writing text in subplots. All the other subplots are for the curves. The matter is that when I write it in a loop (the loop is complicated), all the previous subplots disappear on every iteration. I don't quite understand where my mistake is... Could you please help me to find it, because I am a beginner.)
year=2011;
observatories_name=Table_Help.Get_Names(year);
t=1:1440;
figure(1);
s(1)=subplot(43,13,1);
text(0.5,0.5,'Obs.\Month'); axis off
for i = 2:1:13
s(i)=subplot(43,13,i);
text(0.5,0.5,num2str(i-1)); axis off
end
for j=1:1: length(observatories_name)
s(1+13*j)=subplot(43,13,1+13*j);
text(0.1,0.1,observatories_name{j}); axis off
end
for n = 1:1: length(observatories_name) %Probably, the mistake is in this loop, as on every iteration of THIS loop everything disappears=(((
Curves_data=Table_Help.C_Find(year,observatories_name{n});
for k=2:1:13
curve_forplot=Curves_data(:,k-1);
s(k+n*13)=subplot(43,13, k+n*13);
plot(t,curve_forplot,'b');
xlim([0 1440]);
set(gca,'XTickLabel',{[]});
set(gca,'YTickLabel',{[]});
set(gca,'Xtick',[]);
xlim([0 1440]);
end
end
Here is the code. Table_Help is a class from which I get the names of observatories(Get_Names) and the curves(C_Find). Subplot(43,13,1) is aimed at showing what is inside the rows and columns. /p
So, my problem is the following:</p>
When I pass the first two loops (i and j) everything is OK. Then, when the n-loop starts, everything created in i-and j loops vanishes. Then on every interation of n everything disappears again and again... (And I don't quite follow why...) Though k-loop is working fine. I mean, that MATLAB creates 12 subplots in k-loop, and then on the new interation of n it eliminates them...(
  3 comentarios
Artem Smirnov
Artem Smirnov el 18 de En. de 2017
When I pass the first two loops (i and j) everything is OK. Then, when the n-loop starts, everything created in i-and j loops vanishes. Then on every interation of n everything disappears again and again... Though k-loop is working fine. I mean, that MATLAB creates 12 subplots in k-loop, and then on the new interation of n it eliminates them...(
Carina Schwab
Carina Schwab el 31 de En. de 2019
I observed the same problem. The reason seems to be the line that sets the XTickLabel. If this line is there, then the subplot gets deleted at the next subplot call. If the line is in comment the plots stay. Not sure what causes this and I also wasn't able to produce a minimal example for this phenomenon.

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 18 de En. de 2017
You can use the debugger to find the command, which produces the observed behavior. Set a breakpoint in tzhe first line of the code, start the program and step through the code line by line until the unwanted vanishing happens. Then you can get either an idea of how to solve this or you can provide detailed information here again.
  2 comentarios
Artem Smirnov
Artem Smirnov el 18 de En. de 2017
Editada: Artem Smirnov el 18 de En. de 2017
I added it to the question. And I tried it with a breakpoint. However, I don't see the mistake. I can only guess, that problems begin with the iterations of n, because as this loop starts, everything vanishes. But I don't see where is the mistake.=(((((
Jan
Jan el 18 de En. de 2017
Did you consider, that you overwrite the subplots?
for j = 1:length(observatories_name)
s(1+13*j)=subplot(43,13,1+13*j);
This creates the subplot with index 15 in the second iteration.
for n = 1:1: length(observatories_name)
Curves_data=Table_Help.C_Find(year,observatories_name{n});
for k=2:1:13
...
s(k+n*13)=subplot(43,13, k+n*13);
Now for n=1 and k=2 the 15th subplot is accessed again. Is this wanted? But I think, this is another problem.
Are you really sure, that the figure is cleared in this line:
for n = 1:1: length(observatories_name)
? If not, please use the debugger to find the line, which clears the figure. Perhaps:
Curves_data=Table_Help.C_Find(year,observatories_name{n});
is a function, which contains a clf command? You can check this by not using the "Step" button of the debugger, but "Step in", to proceed with the debuggign in the called function. Please consider that we cannot run your code, because we do not have your data and full code. Therefore it is really up to you either to provide code which we can run or to debug the code more deeply.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by