How to add a legend to all open figures?

24 views (last 30 days)
Morning everyone, I am struggling to reintroduce a legend to all open figures. I have seen a post about how to create a function that accesses the open figure handles, I cannot get it to work.
Below is the code, it essentially creates a tight subplot and then splits it into individual plots (21), these ones are the ones I want to add a legend to. If I appy a legend in the tight subplot (per one) it will distort the imagine and subsquent split. There is a function "Sub_Fig_Divider" that I have also attached with the correction applied.
Any help would be appreciated
Thanks
Richard
fh = figure();
fh.WindowState = 'maximized';
Water_levels = string([{'Low','Med','High'}]); %WL
Cell_ID = string(["Crest", "Av","B","Cv","Ah","Ch","Toe"]);
line_colour = string([{'b-','g-','r-'}]);
Y_units = string(["m","m","m","","","",""]);
soil_consit = "HSMC";
analysis = "Cum. mean period";
headers = ["XDisp", "YDisp", "XY Disp", "XStrain", "YStrain", "XYShearStrain", "DevStrain"];
load'Pd_cum_sum_excel_reorg_2.mat';
rows = size(Pd_cum_sum_excel_reorg_2,1);
columns = size(Pd_cum_sum_excel_reorg_2,2);
[ha,pos] = tight_subplot(rows,columns,[.15 .05],[.1 .1],[.05 .05]);
tot_count = rows*columns;
count =0 ;
row_count =0;
column_count =0;
for aa = 1:tot_count
set(gcf,'color','w')
if aa ==1
row_count = row_count +1;
column_count = column_count+1;
end
axes(ha(aa));
for bb = 1:size(Pd_cum_sum_excel_reorg_2{row_count,column_count},1)
x = 1:numel(Pd_cum_sum_excel_reorg_2{row_count,column_count}(bb,:));
y = Pd_cum_sum_excel_reorg_2{row_count,column_count}(bb,:);
hold on
plot(x,y)
hold off
end
%reset(gca) % Only for my computer
Y_lab = sprintf('%s %s', headers(column_count), Y_units(column_count));
ylabel(Y_lab,'fontName','Times','fontweight','bold','fontangle','italic');
xlabel('Periods','fontName','Times','fontweight','bold','fontangle','italic');
title_format_upper = sprintf('%s - %s',soil_consit,slope,char(176),analysis);
title_format_middle = sprintf('%s - %s',headers(column_count),upper(Water_levels(row_count)));
title_format_lower = sprintf('%s',Cell_ID(column_count));
title(({title_format_upper;title_format_middle;title_format_lower}),'fontName','Times','fontweight','normal','fontangle','italic', 'fontweight','bold');%'Interpreter', 'LaTeX');
if column_count <columns
column_count = column_count+1;
elseif column_count == columns
column_count = 1;
row_count = row_count + 1;
end
end
for aa =1:columns
linkaxes(ha(aa:columns:end),'xy');
end
%%
Sub_Fig_Divider %%%% This is a downloaded function (To work TH2=plot_new(ii-1) needs to be plot_new(ii-1) within code)
figHandles = findall(0,'Type','figure'); %open figures
GIU = figHandles(2:end); % Removes the orginal main subplot for the application below
%%Bit I am struggling on%%
for ii = 1:numel(GIU)
Lgd = legend(ax(ii),headers,'fontweight','bold','box','off','location','bestoutside');
title(lgd,'Water regime','fontweight','bold');
end
%% saving bit

Accepted Answer

Matt J
Matt J on 6 Apr 2021
Edited: Matt J on 6 Apr 2021
For example,
%% Open several figures with subplots
for i=1:2
figure(i)
for j=1:2
subplot(2,1,j)
plot(rand(5,3));
end
end
%% Find all figures and subplots and add legend
H=findobj('Type','figure');
for i=1:numel(H)
ax=findobj(H(i),'Type','axes');
for j=1:numel(ax)
legend(ax(j));
end
end

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!