Add legend to mutiple figures in a loop with conditional

1 view (last 30 days)
Hello,
I am trying to plot a multiple plots in a for loop with a condition. This comes from a struct which has the conditional value, 8 x-value vectors, 8 y-value vectors and a second conditional that is important. This struct is quite huge however so the code goes more or less like this:
for i =1:2000
if dataStruct(i).condition1 == 0
figure(1)
plot(dataStruct(i).x1, dataStruct(i).y1, 'DisplayName', ['with condition 2 =' num2str(dataStruct(i).condition2)])
hold on
figure(2)
plot(dataStruct(i).x2, dataStruct(i).y2, 'DisplayName', ['with condition 2 =' num2str(dataStruct(i).condition2)])
hold on
...
figure(8)
plot(dataStruct(i).x8, dataStruct(i).y8, 'DisplayName', ['with condition 2 =' num2str(dataStruct(i).condition2)])
hold on
end
end
How can I present all the legends on each figure?
Thank you in advance and sorry for not being able to share a better example code as the data cannot be shared right now.

Accepted Answer

Adam Danz
Adam Danz on 28 Sep 2020
Edited: Adam Danz on 28 Sep 2020
I agree with Mario that you just have to provide the axis handle to specify where each legend goes. But there's some other stuff you should clean up, too.
There's also no need to provide any legend strings in the legend() function since you're already defining them using DisplayName which is the best way to do it.
Don't call 'figure' within the loop nor should you call hold on. These are greately slowing down your code. Do that once, before the loop.
% Create all figs and axes; apply hold-on
f(1) = figure();
ax(1) = axes(f(1));
hold(ax(1), 'on')
f(2) = figure();
ax(2) = axes(f(2));
hold(ax(2), 'on')
f(3) = figure();
ax(3) = axes(f(3));
hold(ax(3), 'on')
% Loop through data and specify axes
for i =1:2000
if dataStruct(i).condition1 == 0
plot(ax(1), dataStruct(i).x1, dataStruct(i).y1, 'DisplayName', ['with condition 2 =' num2str(dataStruct(i).condition2)])
plot(ax(2), dataStruct(i).x2, dataStruct(i).y2, 'DisplayName', ['with condition 2 =' num2str(dataStruct(i).condition2)])
plot(ax(3), dataStruct(i).x8, dataStruct(i).y8, 'DisplayName', ['with condition 2 =' num2str(dataStruct(i).condition2)])
end
end
% Add legend
legend(ax(1))
legend(ax(2))
legend(ax(3))
Note that the sections before and after the loop can be placed within their own loops if you're working with many axes. For example,
f = gobjects(10,1);
ax = gobjects(10,1);
for i = 1:10
f(i) = figure();
ax(i) = axes(f(i));
hold(ax(i), 'on')
end

More Answers (1)

Mario Malic
Mario Malic on 28 Sep 2020
Edited: Mario Malic on 28 Sep 2020
Just an example
% This might be better way to do the plotting
fig1 = figure(1)
ax = axes('Parent', fig1)
plot(ax, x,y);
legend(ax,'legendtitle')
Alternative solution, call your figure to be the current one
figure(1) % now it's the current one
legend(gca, 'legendtitle')
figure(2)
legend(gca, 'legend2')
% and so on
Great answer by Walter Roberson, why should you set Parent property to axes.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!