truesize.m
来自「有关matlab的电子书籍有一定的帮助希望有用」· M 代码 · 共 595 行 · 第 1/2 页
M
595 行
function truesize(varargin)
%TRUESIZE Adjust display size of image.
% TRUESIZE(FIG,[MROWS NCOLS]) adjusts the display size of an
% image. FIG is a figure containing a single image or a single
% image with a colorbar. [MROWS MCOLS] is a 1-by-2 vector that
% specifies the requested screen area (in pixels) that the
% image should occupy.
%
% TRUESIZE(FIG) uses the image height and width for
% [MROWS MCOLS]. This results in the display having one screen
% pixel for each image pixel.
%
% If you omit the figure argument, TRUESIZE works on the
% current figure.
%
% Remarks
% -------
% If the 'TruesizeWarning' toolbox preference is 'on', TRUESIZE
% displays a warning if the image is too large to fit on the
% screen. (The entire image is still displayed, but at less
% than true size.) If 'TruesizeWarning' is 'off', TRUESIZE does
% not display the warning. Note that this preference applies
% even when you call TRUESIZE indirectly, such as through
% IMSHOW.
%
% See also IMSHOW, IPTSETPREF, IPTGETPREF.
% Clay M. Thompson 10-15-92
% Revised Steven L. Eddins, September 1996
% Copyright 1993-1998 The MathWorks, Inc. All Rights Reserved.
% $Revision: 5.15 $ $Date: 1997/11/24 15:36:33 $
[axHandle, imHandle, colorbarHandle, imSize, resizeType, msg] = ...
ParseInputs(varargin{:});
if (~isempty(msg))
error(msg);
end
switch resizeType
case 1
% Figure contains one image and nothing else
Resize1(axHandle, imHandle, imSize);
case 2
% Figure contains other noncolorbar axes
% or uicontrols.
Resize2(axHandle, imHandle, imSize);
case 3
% Figure contains one image and a colorbar.
Resize3(axHandle, imHandle, imSize, colorbarHandle);
end
%--------------------------------------------
% Subfunction ParseInputs
%--------------------------------------------
function [axHandle,imHandle,colorbarHandle,imSize,resizeType,msg] = ...
ParseInputs(varargin)
imSize = [];
colorbarHandle = [];
msg = '';
axHandle = [];
imHandle = [];
resizeType = [];
if (nargin == 0)
axHandle = gca;
end
if (nargin >= 1)
if ((nargin == 1) & isequal(size(varargin{1}), [1 2]))
% truesize([M N])
figHandle = gcf;
imSize = varargin{1};
else
% truesize(FIG, ...)
if (~ishandle(varargin{1}) | ~strcmp(get(varargin{1},'type'),'figure'))
msg = 'FIG must be a valid figure handle';
return;
else
figHandle = varargin{1};
end
end
axHandle = get(figHandle, 'CurrentAxes');
if (isempty(axHandle))
msg = 'Current figure has no axes';
return;
end
end
if (nargin >= 2)
imSize = varargin{2};
if (~isequal(size(imSize), [1 2]))
msg = 'REQSIZE must be a 1-by-2 vector';
return;
end
end
figHandle = get(axHandle, 'Parent');
% Find all the images and texturemapped surfaces
% in the current figure. These are the candidates.
h = [findobj(figHandle, 'Type', 'image') ;
findobj(figHandle, 'Type', 'surface', ...
'FaceColor', 'texturemap')];
% If there's a colorbar, ignore it.
colorbarHandle = findobj(figHandle, 'type', 'image', ...
'Tag', 'TMW_COLORBAR');
if (~isempty(colorbarHandle))
for k = 1:length(colorbarHandle)
h(h == colorbarHandle(k)) = [];
end
end
if (isempty(h))
msg = 'No images or texturemapped surfaces in the figure';
return;
end
% Start with the first object on the list as the
% initial candidate. If it's not in the current
% axes, look for another one that is.
imHandle = h(1);
if (get(imHandle,'Parent') ~= axHandle)
for k = 2:length(h)
if (get(h(k),'Parent') == axHandle)
imHandle = h(k);
break;
end
end
end
figKids = allchild(figHandle);
if ((length(findobj(figKids, 'flat', 'Type', 'axes')) == 1) & ...
(length(findobj(figKids, 'flat', 'Type', 'uicontrol', ...
'Visible', 'on')) == 0) & ...
(length(imHandle) == 1))
% Resize type 1
% Figure contains only one axes object, which contains only
% one image.
resizeType = 1;
elseif (isempty(colorbarHandle))
% Figure contains other objects and not a colorbar.
resizeType = 2;
else
% Figure contains a colorbar. Do we have a one image
% one colorbar situation?
if (length(colorbarHandle) > 1)
% No
resizeType = 2;
else
colorbarAxes = get(colorbarHandle, 'Parent');
uimenuKids = findobj(figKids, 'flat', 'Type', 'uimenu');
invisUicontrolKids = findobj(figKids, 'flat', 'Type', 'uicontrol', ...
'Visible', 'off');
otherKids = setdiff(figKids, ...
[uimenuKids ; colorbarAxes ; axHandle ; invisUicontrolKids]);
if (length(otherKids) > 1)
% No
resizeType = 2;
else
% Yes, one image and one colorbar
resizeType = 3;
end
end
end
%--------------------------------------------
% Subfunction Resize1
%--------------------------------------------
function Resize1(axHandle, imHandle, imSize)
% Resize figure containing a single axes
% object with a single image.
if (isempty(imSize))
% How big is the image?
imageWidth = size(get(imHandle, 'CData'), 2);
imageHeight = size(get(imHandle, 'CData'), 1);
else
imageWidth = imSize(2);
imageHeight = imSize(1);
end
if (imageWidth * imageHeight == 0)
% Don't try to handle the degenerate case.
return;
end
axUnits = get(axHandle, 'Units');
set(axHandle, 'Units', 'pixels');
axPos = get(axHandle, 'Position');
% Do we need to do anything?
if (isequal(round(axPos(3:4)), [imageWidth imageHeight]))
% No!
set(axHandle, 'Units', axUnits);
return;
end
figHandle = get(axHandle, 'Parent');
figUnits = get(figHandle, 'Units');
rootUnits = get(0, 'Units');
set(figHandle, 'Units', 'pixels');
figLeftBorder = 10; % assume left figure decorations are 10 pixels
figRightBorder = 10;
figBottomBorder = 10;
figTopBorder = 50;
minFigWidth = 128; % don't try to display a figure smaller than this.
minFigHeight = 128;
% What are the gutter sizes?
figPos = get(figHandle, 'Position');
gutterLeft = max(axPos(1) - 1, 0);
gutterRight = max(figPos(3) - (axPos(1) + axPos(3)) + 1, 0);
gutterBottom = max(axPos(2) - 1, 0);
gutterTop = max(figPos(4) - (axPos(2) + axPos(4)) + 1, 0);
% What are the screen dimensions
screenSize = get(0, 'ScreenSize');
screenWidth = screenSize(3);
screenHeight = screenSize(4);
if ((screenWidth <= 1) | (screenHeight <= 1))
screenWidth = Inf;
screenHeight = Inf;
end
scale = 100;
done = 0;
defAxesPos = get(0,'DefaultAxesPosition');
nonzeroGutters = (gutterLeft > 0);
while (~done)
if (nonzeroGutters)
gutterWidth = round((1 - defAxesPos(3)) * imageWidth / defAxesPos(3));
gutterHeight = round((1 - defAxesPos(4)) * imageHeight / defAxesPos(4));
newFigWidth = imageWidth + gutterWidth;
newFigHeight = imageHeight + gutterHeight;
else
newFigWidth = imageWidth;
newFigHeight = imageHeight;
end
if (((newFigWidth + figLeftBorder + figRightBorder) > screenWidth) | ...
((newFigHeight + figBottomBorder + figTopBorder) > screenHeight))
scale = 3 * scale / 4;
imageWidth = round(imageWidth * scale / 100);
imageHeight = round(imageHeight * scale / 100);
else
done = 1;
end
end
newFigWidth = max(newFigWidth, minFigWidth);
newFigHeight = max(newFigHeight, minFigHeight);
figPos(1) = max(1, figPos(1) - floor((newFigWidth - figPos(3))/2));
figPos(2) = max(1, figPos(2) - floor((newFigHeight - figPos(4))/2));
figPos(3) = newFigWidth;
figPos(4) = newFigHeight;
% Translate figure position if necessary
deltaX = (screenSize(3) - figRightBorder) - (figPos(1) + figPos(3));
if (deltaX < 0)
figPos(1) = figPos(1) + deltaX;
end
deltaY = (screenSize(4) - figTopBorder) - (figPos(2) + figPos(4));
if (deltaY < 0)
figPos(2) = figPos(2) + deltaY;
end
% Figure out where to place the axes object in the
% resized figure
gutterWidth = figPos(3) - imageWidth;
gutterHeight = figPos(4) - imageHeight;
gutterLeft = floor(gutterWidth/2);
gutterBottom = floor(gutterHeight/2);
axPos(1) = gutterLeft + 1;
axPos(2) = gutterBottom + 1;
axPos(3) = imageWidth;
axPos(4) = imageHeight;
set(figHandle, 'Position', figPos)
set(axHandle, 'Position', axPos);
% Restore the units
drawnow; % necessary to work around HG bug -SLE
set(figHandle, 'Units', figUnits);
set(axHandle, 'Units', axUnits);
set(0, 'Units', rootUnits);
% Set the nextplot property of the figure so that the
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?