2
votes

I am using the suptitle.m file to add a main title on a figure with 3 subplots. Each subplot has its own legend. After I issue the suptitle command, the legend for the last plot disappears. Has anyone else seen this before? How do I fix this?

The same question was asked 10 years ago on Matworks, with no answers.

http://www.mathworks.com/matlabcentral/newsreader/view_thread/44980

Extra

If I plot the figure, and then manipulate the legends positions. And then sets the suptitle, it is the last legend that has been manipulated, which is hidden behind the plots. This would seem like some information is hidden in memory that is used by suptitle.m

3
If you look in the code you can see that suptitle() is a helper function for a demo and that some parameters are set manually inside the code and this is probably what causes your problem. It even comes with a warning: "Warning: If the figure or axis units are non-default, this will break." The workaround given by @Naveen below should work. - Ghaul
@Ghaul true I read that also. I just wanted to know if others had found a solution. And as stated in the comment below, if you do as Naveen states, then the title of the subplot and the suptitle will be ontop of each other. - JTIM

3 Answers

1
votes

Instead of replacing the last plot after writing the suptitle, use it this way:

figure()
suptitle('Your Title');
subplot(3,1,1)
plot.....
subplot(3,1,2)
plot.....
subplot(3,1,3)
plot.....

This way the suptitle does not interfere with the legends of any of the subplots.

1
votes

Zhang Jiang provided a solution for the disappearing legend in suptitle as part of an x-ray toolbox. Basically, you just need to include the following line at the end of suptitle.m:

% fix legend if one exists
legH = legend;
if ~isempty(legH)
    axes(legH);
end

This will re-activate the missing legend.

Follow Jiang's implementation of suptitle.m. Search the script for "legend" to find the piece of code with the solution.

Sauro Salomoni

0
votes

Solution I have looked at the code and have not been able to solve the issue. But by creating suptitle before the last plot it works

figure()
subplot(3,1,1)
plot.....
subplot(3,1,2)
plot.....
subplot(3,1,3)
suptitle('Your Title');
plot.....