Error using sgtitle?

23 views (last 30 days)
I have a subplot with four graphs I'm trying to put my figures title over..
sgtitle('Mean Times per Group',['Over 37.5 = ' num2str(meanover) 'Under 37.5 = ' num2str(meanunder)]);
It accepts the "Mean Times per Group" part but gives me the error below.
Error using matlab.graphics.illustration.subplot.Text/set
Unrecognized property Over 37.5 = 0.97049Under 37.5 = 0.99391 for class Text.
Error in sgtitle (line 98)
set(h, pvpairs{:});
Any ideas why?

Accepted Answer

Walter Roberson
Walter Roberson on 5 Dec 2021
So if you have exactly two arguments like you do, the first one would have to be target . But 'Mean Times per Group' is not a target. So sgtitle() keeps parsing to figure out what the parameters are.
The next possibility is that the second parameter is one of the permitted Name options. But it is not -- there is no parameter named 'Over 37.5 = 0.97049Under 37.5 = 0.99391' .
What you probably want is
sgtitle({'Mean Times per Group',['Over 37.5 = ' num2str(meanover) 'Under 37.5 = ' num2str(meanunder)]});
Reminder though that you can code that more clealy as
sgtitle({'Mean Times per Group', "Over 37.5 = " + meanover + "Under 37.5 = " + meanunder});

More Answers (1)

Voss
Voss on 5 Dec 2021
sgtitle is interpreting the second argument you give it (i.e., ['Over 37.5 = ' num2str(meanover) 'Under 37.5 = ' num2str(meanunder)]) as a property name. That is, it expects property-value pairs after the first argument, in this case.
I don't have access to sgtitle, so I can't say for sure what the solution is, but you can try sending a cell array of character arrays as the first argument, if this is meant to be a two-line title. Like this:
sgtitle({'Mean Times per Group',['Over 37.5 = ' num2str(meanover) 'Under 37.5 = ' num2str(meanunder)]});
Or if that doesn't work, you can send a character array with a newline in it, like this:
sgtitle(sprintf('Mean Times per Group\n%s',['Over 37.5 = ' num2str(meanover) 'Under 37.5 = ' num2str(meanunder)]));

Community Treasure Hunt

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

Start Hunting!

Translated by