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.

采纳的回答

Cris LaPierre
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
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).
Carlos Correia
Carlos Correia 2020-3-26
That works as well and is exactly what I was looking for as it allows me to do everything inside the function.
Thank you both once again for your help!

请先登录,再进行评论。

更多回答(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