Error using sgtitle?

31 次查看(过去 30 天)
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?

采纳的回答

Walter Roberson
Walter Roberson 2021-12-5
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});

更多回答(1 个)

Voss
Voss 2021-12-5
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