Add legend to mutiple figures in a loop with conditional
Show older comments
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
More Answers (1)
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
1 Comment
Guilherme Theis
on 28 Sep 2020
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!