Having multiple legends in subplots generated by for loop and nested if loop.

15 次查看(过去 30 天)
I have a 2x2 plot generated by for loop and nested if loops. Each subplot has more than one data set however I am struggling to get multiple legend shown in each subplot. How do I get multiple legends in every subplot?
Below is the code used and the figure attached.
Cheers
BasePath = *folder directory*
files_in_this_folder = dir([BasePath,'\*.xlsx']); %Change .xlsx to .xls when necessary
number_of_files = length(files_in_this_folder);
for index = 1:number_of_files
filename = files_in_this_folder(index).name;
if strfind(filename(9),'F')
if strfind(filename(12),'0')
num = xlsread(filename);
figure(1)
subplot(2,2,1)
n = filename(5:7)
p = plot(num(6:end,2:2),num(6:end,7:7),'x','DisplayName',num2str(n))
xlabel('trackscale')
ylabel('tonnes removed')
title(strcat(filename(9),'-',filename(11:12)))
hold all
% % Plot for Perfect Controller
x = 123: 144;
y = x-123;
r = round(max(num(6:end,7:7))+0.5);
plot(x,y,'r')
ylim([0 r])
legend(p(1,:),'Location','northwest')
else
num = xlsread(filename);
subplot(2,2,2)
n = filename(5:7)
p = plot(num(6:end,2:2),num(6:end,7:7),'x','DisplayName',num2str(n))
xlabel('trackscale')
ylabel('tonnes removed')
title(strcat(filename(9),'-',filename(11:12)))
hold all
% % Plot for Perfect Controller
x = 123: 144;
y = x-123;
r = round(max(num(6:end,7:7))+0.5);
plot(x,y,'r')
ylim([0 r])
legend(p(1,:),'Location','northwest')
end
elseif strfind(filename(9),'L') & strfind(filename(11:12),'50')
num = xlsread(filename);
subplot(2,2,3)
n = filename(5:7)
p = plot(num(6:end,2:2),num(6:end,7:7),'x','DisplayName',num2str(n))
xlabel('trackscale')
ylabel('tonnes removed')
title(strcat(filename(9),'-',filename(11:12)))
hold all
% % Plot for Perfect Controller
x = 123: 144;
y = x-123;
r = round(max(num(6:end,7:7))+0.5);
plot(x,y,'r')
ylim([0 r])
legend(p(1,:),'Location','northwest')
else %strfind(filename(9),'L') & strfind(filename(11:12),'55')
num = xlsread(filename);
subplot(2,2,4)
n = filename(5:7)
p = plot(num(6:end,2:2),num(6:end,7:7),'x','DisplayName',num2str(n))
xlabel('trackscale')
ylabel('tonnes removed')
title(strcat(filename(9),'-',filename(11:12)))
hold all
% % Plot for Perfect Controller
x = 123: 144;
y = x-123;
r = round(max(num(6:end,7:7))+0.5);
plot(x,y,'r')
ylim([0 r])
legend(p(1,:),'Location','northwest')
end
end

采纳的回答

Walter Roberson
Walter Roberson 2015-5-14
you are only calling legend() once for every subplot, and you are specifying p(1,:) as the children to form the legend out of. Each of your p is formed as
p = plot(num(6:end,2:2),num(6:end,7:7),'x','DisplayName',num2str(n))
The 2:2 and the 7:7 there result in a column vector for the x value and a column vector for the y value, so you are plotting one vector against another, which is an operation that is going to produce a single line object rather than an array of line objects. Therefore all of your p are going to be scalars that are line objects, and you are requesting that only those be included in the legend.
If you want the plot(x,y) to be included in the legend, the easiest method would be to use
thisax = subplot(2,2,3); %or as appropriate
and
legend(thisax, 'location', 'northwest')
so that all of the children of the axes are annotated. But if that is what you want you should include a Displayname on the plot(x,y) so the legend will know what to write.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by