Multiple plots in the same figure

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.

 采纳的回答

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 个评论

Doing that solves the issue and I get ony 1 figure with all the plots but It gives me the second problem I mentioned, I only have the legend for the 3rd time I run the function, is there a way to get all 3 legends?
I would expect to have all 3 since each run of the function has a different legend
What should the final legend look like?
I would like something like this:
color1 material1
color2 material2
color3 material3
as of now I only get
color3 material3
The simplest way would be to not add the legend in your function. Instead, add the legend afterwards. By default, legend is going to add labels to the lines in the same order they were plotted. You have 5 lines added each time your function is called, but only want 3 legend entries. Use the graphic objects you extract to tell it which line style to match to each label.
legend([Plotting1(1) Plotting2(1) Plotting3(1)],{'Label1','Label2','Label3'})
That works, thank you for your help!
Try
for n=1:5
% other commands
Plotting(n)=plot(x,y,chosencolor,'DisplayName',material)
Plotting(n).Annotation.LegendInformation.IconDisplayStyle = 'off';
end
Plotting(1).Annotation.LegendInformation.IconDisplayStyle = 'on';
legend show
At the moment, I cannot promise that extra legend entries will not show up.
It would be easier if you could gather all of the Plotting objects in the calling routine, and legend() only the first one of each group.
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).
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 个)

类别

标签

Community Treasure Hunt

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

Start Hunting!

Translated by