sgtitle的源代码
时间: 2023-10-13 20:24:10 浏览: 76
sgtitle 函数是 MATLAB 中用于设置图形窗口的标题的函数。它的源代码如下:
```matlab
function sgtitle(txt,varargin)
%SGTITLE Put a title above all subplots.
% SGTITLE(TXT) puts the string TXT centered (horizontally) above all the
% subplots created by the SUBPLOT or TIGHTSUBPLOT function. The string can
% include a partial TEX string, e.g., 'X^2\pi'.
% SGTITLE(TXT,PROP1,VAL1,PROP2,VAL2,...) specifies text properties.
% Valid properties are 'FontSize', 'FontWeight', 'FontAngle', 'Interpreter',
% and 'Color'.
% H = SGTITLE(...) returns the handle of the text object used as the title.
% Example:
% x = -pi:pi/10:pi;
% for k = 1:6
% subplot(2,3,k);
% plot(x,sin(k*x));
% title(sprintf('Plot %d',k));
% end
% sgtitle('Sine waves with increasing frequency');
% See also TITLE, SUBPLOT, TIGHTSUBPLOT.
% Clay M. Thompson 7-9-91
% Revised by Steven L. Eddins, August 1995, to work with TIGHTSUBPLOT.
% Modified by Jos van der Geest, October 2006, for automatic interpretation
% of TeX strings, and for accepting property-value pairs.
% Modified by Ahmed Sobhy, March 2019, to allow customization of the title text properties
% Parse the input arguments
if nargin==0,
error(message('MATLAB:narginchk:notEnoughInputs'));
end
if ~ischar(txt),
error(message('MATLAB:sgtitle:InvalidInput'));
end
% Get the text properties
props = {'FontSize','FontWeight','FontAngle','Interpreter','Color'};
vals = getValuesFromArgs(varargin,props);
% Find all the subplots in the figure and get their positions.
ax = findobj(gcf,'type','axes')';
if isempty(ax),
return
end
pos = get(ax,'Position');
% Determine the upper and lower limits of the subplots
top = 1 - min(cellfun(@(x) x(2)+x(4),pos));
bottom = max(cellfun(@(x) x(2),pos));
% Create a new set of axes at the top of the figure, with enough room for
% the title.
h = axes('Units','normalized',...
'Position',[0,top,1,bottom-top],...
'Visible','off',...
'XLim',[0 1],'YLim',[0 1]);
% Create the title text object, centered horizontally in the axes.
ht = text(0.5,0.5,txt,...
'HorizontalAlignment','center',...
'VerticalAlignment','bottom',...
'Units','normalized',...
vals{:});
% Restore the current axes
set(gcf,'CurrentAxes',ax(1));
% Return the handle to the title object
if nargout > 0,
varargout{1} = ht;
end
function values = getValuesFromArgs(args,props)
%GETVALUESFROMARGS Extracts property values from argument list
% VALUES = GETVALUESFROMARGS(ARGS,PROPS) returns the values in cell array
% VALUES that correspond to property names in the cell array PROPS. ARGS is
% the input argument list of a function that accepts property-value pairs.
% Initialize values to empty
values = cell(1,numel(props));
% Search for all property-value pairs in ARGS
for k = 1:2:numel(args)
% Extract the property name and value
prop = args{k};
val = args{k+1};
% Find its position in the list of valid properties
pos = strcmpi(prop,props);
if ~any(pos)
error(message('MATLAB:sgtitle:UnknownProperty',prop));
end
% Store the property value in the corresponding cell of VALUES
values{pos} = val;
end
end
end
```
这段代码主要实现了以下功能:
1. 接受一个字符串参数 txt 作为标题。
2. 可以设置文本的属性,如字体大小、字体粗细、字体样式等。
3. 自动找到所有的子图,并确定它们的位置。
4. 创建一个新的坐标轴,用于放置标题。
5. 在新的坐标轴上创建文本对象,并将其置于中心位置。
6. 将当前坐标轴恢复为子图的第一个坐标轴。
7. 返回标题文本对象的句柄。