Hold on issue for subplots
Show older comments
Hello,
I had created 3 figures consisting of subplots: 4x2; 6x2 and 4x2 and saved them in 3 seperate .fig files, no problem on this step. The thing is, I need to use those saved figures again and add other plots without deleting the existing ones (directly on existing data, imagine you have a sine wave on plot 4,2,1 and then you are adding a cosine wave; now you have 2 waves inside this plot). For the figures sized 4x2; I do not have any issues but for the figure sized 6x2, whenever I try to add another graph, the existing one goes away. Actually all the settings are going away (title, axes names, etc...) and the new graphs are added on a blank subplot. Basically "hold on" does not work even I did everything the same as I did for the other figures. I researched about this issue and it is mentioned that "SUBPLOT clears the axes when called unless the new subplot properties (such as 'position') match the original subplot properties". Eventually I tried to create the new figure with the exact same graphs and properties, the issue persists. The piece of code that works on the first figure but not for the second is: (By the way I am using Matlab 2013b, so not every function is available for me...)
numX = open(name_figX_final);
figure(numX); pause(2); hold on
The code in summary:
while(a<=length(name_signals_array))
if a==1
num1 = open(name_fig1_final);
figure(num1); pause(2); hold on
subplot(421),
plot(wind_list, signals_stats(1,:), '--ro'); set(gca,'XTick',wind_label); hold on
plot(wind_list, signals_stats(2,:), '--r^'); plot(wind_list, signals_stats(3,:), '--rv');
title('Signals421'); xlabel(prop_xlabel); ylabel('rpm'); legend({'min:','max:', 'mean:'},'Location','best')
subplot(422),
plot(wind_list, signals_stats(4,:), '--ro'); % set(gca,'XTick',wind_label);
title('Signals421'); xlabel(prop_xlabel); ylabel('rpm'); legend({'std:'},'Location','best')
elseif a==2
subplot(423);
plot(wind_list, signals_stats(5,:), '--bo'); set(gca,'XTick',wind_label); hold on
plot(wind_list, signals_stats(6,:), '--b^'); plot(wind_list, signals_stats(7,:), '--bv');
title('Signals423'); xlabel(prop_xlabel); ylabel('deg'); legend({'min:','max:', 'mean:'},'Location','best')
subplot(424),
plot(wind_list, signals_stats(8,:), '--bo'); set(gca,'XTick',wind_label);
title('Signals424'); xlabel(prop_xlabel); ylabel('deg'); legend({'std:'},'Location','best')
elseif a==3
subplot(425),
% .....
subplot(426),
% .....
elseif a==4
subplot(427),
% ....
subplot(428),
% ....
savefig(name_fig1);
end
if a==5
num2 = open(name_fig2_final);
figure(num2); pause(2); hold on
subplot(6,2,1)
plot(wind_list, signals_stats(17,:), '--bo'); set(gca,'XTick',wind_label); hold on
plot(wind_list, signals_stats(18,:), '--b^'); plot(wind_list, signals_stats(19,:), '--bv');
title('Signals621'); xlabel(prop_xlabel); ylabel('Nm'); legend({'min:','max:', 'mean:'},'Location','best')
subplot(6,2,2),
plot(wind_list, signals_stats(20,:), '--bo'); set(gca,'XTick',wind_label);
title('Signals622'); xlabel(prop_xlabel); ylabel('Nm'); legend({'std:'},'Location','best')
elseif a==6
subplot(6,2,3)
plot(wind_list, signals_stats(21,:), '--bo'); set(gca,'XTick',wind_label); hold on
plot(wind_list, signals_stats(22,:), '--b^'); plot(wind_list, signals_stats(23,:), '--bv');
title('Signals623'); xlabel(prop_xlabel); ylabel('Nm'); legend({'min:','max:', 'mean:'},'Location','best')
subplot(6,2,4),
plot(wind_list, signals_stats(24,:), '--bo'); set(gca,'XTick',wind_label);
title('Signals624'); xlabel(prop_xlabel); ylabel('Nm'); legend({'std:'},'Location','best')
elseif a==7
subplot(6,2,5)
%...
subplot(6,2,6),
%...
elseif a==8
subplot(6,2,7)
%...
subplot(6,2,8),
%...
elseif a==9
subplot(6,2,9)
%...
subplot(6,2,10),
%...
elseif a==10
subplot(6,2,11)
%...
subplot(6,2,12),
%...
savefig(name_fig2);
end
a=a+1;
end
2 Comments
Bjorn Gustavsson
on 7 Sep 2021
What I do to avoid these type of problems is to keep the axes-handles of the different subplots and instead of calling subplot again to make one subplot-axes the current I call axes(sph42(1)):
sph42 = subplot(4,2,1);
sph42(2) = subplot(4,2,2);
for i1 = 8:-1:1
sph42(i1) = subplot(4,2,i1)
end
axes(sph42(1))
plot(randn(23))
% etc
If that works with data saved in .fig-format I don't know, but maybe...
HTH
Gulfer Ozcetin
on 8 Sep 2021
Accepted Answer
More Answers (1)
MICHAEL MUTWIRI
on 7 Sep 2021
Edited: MICHAEL MUTWIRI
on 7 Sep 2021
The subplot has a default of replacing old plot when creating a new plot on the same axis. Add the
subplot(abc, 'NextPlot','add') for each subplot case. You also need to hold on after each subplot and hold off before moving to the next subplot
My code contains sample data to demonstrate a figure with subplots can be saved and opened later for additional plots. I have commented out axis labels since I don't your actual data.
Following your while-loop code, you need to have created the two figures and saved them before opening them inside the while loop
Hope your understand and complete the code for if-elseif conditions for a==5 to a==10
name_fig1_final='name_fig1_final.fig';
name_fig2_final='name_fig2_final.fig';
%% prepare some sample data for testing
name_signals_array =1:24;
wind_list = linspace(0,2*pi,100);
signals_stats = ones(30,length(wind_list));
for i=1:size(signals_stats,1)
signals_stats(i,:)=sin(wind_list+i*0.1*pi);
end
%% name_fig1_final: subplot 4x2
num1 = figure;
subplot(4,2,1);z1 = cos(wind_list);plot(wind_list,z1,'r');grid on
subplot(4,2,2);z2 = cos(wind_list);plot(wind_list,z2,'r');grid on
subplot(4,2,3);z3 = cos(wind_list);plot(wind_list,z3,'r');grid on
subplot(4,2,4);z4 = cos(wind_list);plot(wind_list,z4,'r');grid on
subplot(4,2,5);z5 = cos(wind_list);plot(wind_list,z5,'r');grid on
subplot(4,2,6);z6 = cos(wind_list);plot(wind_list,z6,'r');grid on
subplot(4,2,7);z7 = cos(wind_list);plot(wind_list,z7,'r');grid on
subplot(4,2,8);z8 = cos(wind_list);plot(wind_list,z8,'r');grid on
savefig(num1,name_fig1_final)
close(num1)
%% name_fig2_final: subplot 6x2
num2 = figure;
subplot(6,2,1);zz1 = cos(wind_list);plot(wind_list,zz1,'r');grid on
subplot(6,2,2);zz2 = cos(wind_list);plot(wind_list,zz2,'r');grid on
subplot(6,2,3);zz3 = cos(wind_list);plot(wind_list,zz3,'r');grid on
subplot(6,2,4);zz4 = cos(wind_list);plot(wind_list,zz4,'r');grid on
subplot(6,2,5);zz5 = cos(wind_list);plot(wind_list,zz5,'r');grid on
subplot(6,2,6);zz6 = cos(wind_list);plot(wind_list,zz6,'r');grid on
subplot(6,2,7);zz7 = cos(wind_list);plot(wind_list,zz7,'r');grid on
subplot(6,2,8);zz8 = cos(wind_list);plot(wind_list,zz8,'r');grid on
subplot(6,2,9);zz9 = cos(wind_list);plot(wind_list,zz9,'r');grid on
subplot(6,2,10);zz10 = cos(wind_list);plot(wind_list,zz10,'r');grid on
subplot(6,2,11);zz11 = cos(wind_list);plot(wind_list,zz11,'r');grid on
subplot(6,2,12);zz12 = cos(wind_list);plot(wind_list,zz12,'r');grid on
savefig(num2,name_fig2_final)
close(num2)
%
a=1;
while(a<=length(name_signals_array))
if a==1
% at this stage, the figure need to been created and saved before
% trying to open it.
num1 = open(name_fig1_final);
figure(num1); pause(2);
subplot(421,'NextPlot','add'),
hold on
plot(wind_list, signals_stats(1,:), '--b'); %set(gca,'XTick',wind_label);
plot(wind_list, signals_stats(2,:), '--m'); plot(wind_list, signals_stats(3,:), '--g');
title('Signals421');% xlabel(prop_xlabel); ylabel('rpm'); legend({'min:','max:', 'mean:'},'Location','best')
hold off
subplot(422,'NextPlot','add'),
hold on
plot(wind_list, signals_stats(4,:), '--b'); % set(gca,'XTick',wind_label);
title('Signals422'); %xlabel(prop_xlabel); ylabel('rpm'); legend({'std:'},'Location','best')
hold off
elseif a==2
subplot(423, 'NextPlot','add');
hold on
plot(wind_list, signals_stats(5,:), '--b'); %set(gca,'XTick',wind_label); hold on
plot(wind_list, signals_stats(6,:), '--g'); plot(wind_list, signals_stats(7,:), '--c');
title('Signals423'); %xlabel(prop_xlabel); ylabel('deg'); legend({'min:','max:', 'mean:'},'Location','best')
hold off
subplot(424, 'NextPlot','add'),
hold on
plot(wind_list, signals_stats(8,:), '--b'); %set(gca,'XTick',wind_label);
title('Signals424'); %xlabel(prop_xlabel); ylabel('deg'); legend({'std:'},'Location','best')
hold off
elseif a==3
subplot(425, 'NextPlot','add'),
% .....
subplot(426, 'NextPlot','add'),
% .....
elseif a==4
subplot(427, 'NextPlot','add'),
% ....
subplot(428, 'NextPlot','add'),
% ....
%savefig(name_fig1); take this outside the if-elseif conditions
end
savefig(num1,name_fig1_final)
% Edit the code below in a similar fashion to the avove part
% if a==5
% num2 = open(name_fig2_final);
% figure(num2); pause(2); hold on
% subplot(6,2,1)
% plot(wind_list, signals_stats(17,:), '--bo'); set(gca,'XTick',wind_label); hold on
% plot(wind_list, signals_stats(18,:), '--b^'); plot(wind_list, signals_stats(19,:), '--bv');
% title('Signals621'); xlabel(prop_xlabel); ylabel('Nm'); legend({'min:','max:', 'mean:'},'Location','best')
% subplot(6,2,2),
% plot(wind_list, signals_stats(20,:), '--bo'); set(gca,'XTick',wind_label);
% title('Signals622'); xlabel(prop_xlabel); ylabel('Nm'); legend({'std:'},'Location','best')
% elseif a==6
% subplot(6,2,3)
% plot(wind_list, signals_stats(21,:), '--bo'); set(gca,'XTick',wind_label); hold on
% plot(wind_list, signals_stats(22,:), '--b^'); plot(wind_list, signals_stats(23,:), '--bv');
% title('Signals623'); xlabel(prop_xlabel); ylabel('Nm'); legend({'min:','max:', 'mean:'},'Location','best')
% subplot(6,2,4),
% plot(wind_list, signals_stats(24,:), '--bo'); set(gca,'XTick',wind_label);
% title('Signals624'); xlabel(prop_xlabel); ylabel('Nm'); legend({'std:'},'Location','best')
% elseif a==7
% subplot(6,2,5)
% %...
% subplot(6,2,6),
% %...
% elseif a==8
% subplot(6,2,7)
% %...
% subplot(6,2,8),
% %...
% elseif a==9
% subplot(6,2,9)
% %...
% subplot(6,2,10),
% %...
% elseif a==10
% subplot(6,2,11)
% %...
% subplot(6,2,12),
% %...
% savefig(name_fig2);
% end
%fprintf('a = %g\n',a)
a=a+1;
end
1 Comment
Gulfer Ozcetin
on 8 Sep 2021
Edited: Gulfer Ozcetin
on 8 Sep 2021
Categories
Find more on Subplots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


