Multiple plots in the same figure
6 次查看(过去 30 天)
显示 更早的评论
I have a complex function:
function [FinalResults,Plotting]=Dicom_Reading(Variables)
Plotting=gobjects(5,1);% declare the graphics object
figure
hold on
.
.
.
% other commands
.
.
.
for n=1:5
% other commands
Plotting(n)=plot(x,y,chosencolor,'DisplayName',material)
legend(Plotting(1))% I do this get only 1 legend as they are all the same
end
x and y are different each time.
chosencolor is a string choosing the color.
material is a string with what I would like to have in the legend.
I believe the function is set up correctly as I do indeed get a figure with only 1 legend and 5 curves all of the same color.
I would like to run the function 3 different times for different variables and afterwards combine all the plots in the same graph so that I can compare them.
I expected that
[FinalResults1,Plotting1]=Dicom_Reading(Variables1)
[FinalResults2,Plotting2]=Dicom_Reading(Variables2)
[FinalResults3,Plotting3]=Dicom_Reading(Variables3)
hold on
Plotting1;
Plotting2;
Plotting3;
would work but it is not doing anything, I still get 3 different figures.
Maybe it has something to do with the fact that Plotting1, Plotting2 and Plotting 3 return as 5x1 Line arrays instead of plots?
I also tried changing the code in the function itself by switching the place of the hold command to
for n=1:5
% other commands
hold on
Plotting(n)=plot(x,y,chosencolor,'DisplayName',material)
legend(Plotting(1))% I do this get only 1 legend as they are all the same
end
This does in fact allow me to get all the 15 plots in the same graph but now I only get the legend for the 3rd plot.
I would like to know if there is a solution to this, either combining the 3 plots in the first case or getting all 3 legends in the second case.
0 个评论
采纳的回答
Cris LaPierre
2020-3-26
You have a figure command in your Dicom_Reading function. This will create a new figure window. This is why, when you run the function 3 times, you get 3 separate figures.
Best practide is to always have a hold off command to balance out a hold on. You could put one at the bottom of your function.
function [FinalResults,Plotting]=Dicom_Reading(Variables)
.
.
.
% other commands
.
.
.
Plotting=gobjects(5,1);% declare the graphics object
hold on
for n=1:5
% other commands
Plotting(n)=plot(x,y,chosencolor,'DisplayName',material)
legend(Plotting(1))% I do this get only 1 legend as they are all the same
end
hold off
8 个评论
Cris LaPierre
2020-3-26
I believe that's what my solution does (gather the plotting objects in the calling routine and legend just the first one of each group).
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!