MATLAB Answers

Multiple plots in the same figure

11 views (last 30 days)
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 Comments

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 26 Mar 2020
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 Comments

Show 5 older comments
Walter Roberson
Walter Roberson on 26 Mar 2020
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.
Cris LaPierre
Cris LaPierre on 26 Mar 2020
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 on 26 Mar 2020
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!

Sign in to comment.

More Answers (0)

Sign in to answer this question.