Legend of subplot duplicates entry after for loop
Show older comments
I have this code for some plots that are created in a for loop. this "Real_Time_Plots" function gets called for every time step of the simulation. The problem i am having is that as it is every time the function gets called, the legend of the subplot gets duplicated (Example: First function call the legend has the following entries: Pr1, Pr2, Pr3. On the Second call the legend is double the length with following entries: Pr1, Pr2, Pr3, Pr1, Pr2, Pr3 eventho no new lines were added to the plot ). Am i writing the "hold on's" and "hold off's" at the wrong place?
function Real_Time_Plots(System_Handle, TimeStep_Count )
%Subplots get created
if (isfield(System_Handle.Cache_DynamicVariables,'f1'))
elseif ~(isfield(System_Handle.Cache_DynamicVariables,'f1'))
System_Handle.Cache_DynamicVariables.f1 = figure(1);
ax1 = subplot( 2,3,1 );
ax2 = subplot( 2,3,2 );
ax3 = subplot( 2,3,3 );
ax4 = subplot( 2,3,4 );
ax5 = subplot( 2,3,5 );
ax6 = subplot( 2,3,6 );
System_Handle.Cache_DynamicVariables.child_handles = get(System_Handle.Cache_DynamicVariables.f1,'Children');
else
end
%Name and color array initialization
precipitates = size(System_Handle.Precipitates, 1);
names = cell(precipitates,1);
cc = jet(precipitates);
for i=1:1:precipitates
names{i}=strcat('Pr', num2str(i));
end
%First Subplot
xlim(System_Handle.Cache_DynamicVariables.child_handles(1,1), [0, t_end]);
xlabel(System_Handle.Cache_DynamicVariables.child_handles(1,1), 'time in s' );
ylabel(System_Handle.Cache_DynamicVariables.child_handles(1,1), 'mean radius in m' );
set(System_Handle.Cache_DynamicVariables.child_handles(1,1),'XScale','log');
set(System_Handle.Cache_DynamicVariables.child_handles(1,1),'YScale','log');
set(System_Handle.Cache_DynamicVariables.child_handles(1,1),'FontSize',16);
hold(System_Handle.Cache_DynamicVariables.child_handles(1,1), 'on' );
for i=1:1:precipitates
if System_Handle.Precipitates{i, 1}.Physical_Properties.IsClusterPhaseType
plot(System_Handle.Cache_DynamicVariables.child_handles(1,1), ...
... Time(1:TimeStep_Count,1), MeanRadius.DataTable(1:TimeStep_Count,3 ), 'DisplayName', names{i} , 'color', cc(i,:) , 'LineStyle', '-');
end
end
legend(System_Handle.Cache_DynamicVariables.child_handles(1,1),'Location','best');
hold(System_Handle.Cache_DynamicVariables.child_handles(1,1), 'off' );
%Code for the rest of subplots
drawnow
end
Accepted Answer
More Answers (0)
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!