Add legend to mutiple figures in a loop with conditional
이전 댓글 표시
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.
채택된 답변
추가 답변 (1개)
Mario Malic
2020년 9월 28일
편집: Mario Malic
2020년 9월 28일
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
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!