truesize.m

来自「有关matlab的电子书籍有一定的帮助希望有用」· M 代码 · 共 595 行 · 第 1/2 页

M
595
字号
% axes object gets deleted and replaced for the next plot.
% That way, the new plot gets drawn in the default position.
set(figHandle, 'NextPlot', 'replacechildren');

% Warn if the display is not truesize, unless warning is disabled
% according to toolbox preference setting.
if ((scale < 100) & strcmp(iptgetpref('TruesizeWarning'), 'on'))
    message = ['Image is too big to fit on screen; ', ...
                sprintf('displaying at %d%% scale.', floor(scale))];
    warning(message);
end

%--------------------------------------------
% Subfunction Resize2
%--------------------------------------------
% Resize figure containing multiple axes or
% other objects.  Basically we're going to
% compute a global figure scaling factor
% that will bring the target image into
% truesize mode.  This works reasonably well
% for subplot-type figures as long as all
% the images have the same size.  This is
% basically the guts of truesize.m from IPT
% version 1.
function Resize2(axHandle, imHandle, imSize)

if (isempty(imSize))
    imSize = size(get(imHandle, 'CData'));
end

if (prod(imSize) == 0)
    % Don't try to handle the degenerate case.
    return;
end

axUnits = get(axHandle, 'Units');
set(axHandle, 'Units', 'pixels');
axPosition = get(axHandle, 'Position');
% Do we need to do anything?
if (isequal(round(axPosition(3:4)), [imSize(2) imSize(1)]))
    % No!
    set(axHandle, 'Units', axUnits);
    return;
end

figHandle = get(axHandle, 'Parent');
figUnits = get(figHandle, 'Units');
rootUnits = get(0,'Units');
set(axHandle, 'Units', 'normalized');
axPosition = get(axHandle, 'Position');
set([figHandle 0], 'Units', 'pixels');
figPosition = get(figHandle, 'Position');
screenSize = get(0,'ScreenSize');

% What should the new figure size be?
dx = ceil(imSize(2)/axPosition(3) - figPosition(3));
dy = ceil(imSize(1)/axPosition(4) - figPosition(4));
newFigWidth = figPosition(3) + dx;
newFigHeight = figPosition(4) + dy;

% Is the new figure size too big or too small?
figLeftBorder = 10;
figRightBorder = 10;
figBottomBorder = 10;
figTopBorder = 50;
minFigWidth = 128;
minFigHeight = 128;

if ((newFigWidth + figLeftBorder + figRightBorder) > screenSize(3))
    scaleX = (screenSize(3) - figLeftBorder - figRightBorder) / newFigWidth;
else
    scaleX = 1;
end

if ((newFigHeight + figBottomBorder + figTopBorder) > screenSize(4))
    scaleY = (screenSize(4) - figBottomBorder - figTopBorder) / newFigHeight;
else
    scaleY = 1;
end

if (newFigWidth < minFigWidth)
    scaleX = minFigWidth / newFigWidth;
end
if (newFigHeight < minFigHeight)
    scaleY = minFigHeight / newFigHeight;
end

if (min(scaleX, scaleY) < 1)
    % Yes, the new figure is too big for the screen.
    scale = min(scaleX, scaleY);
    newFigWidth = floor(newFigWidth * scale);
    newFigHeight = floor(newFigHeight * scale);
elseif (max(scaleX, scaleY) > 1)
    % Yes, the new figure is too small.
    scale = max(scaleX, scaleY);
    newFigWidth = floor(newFigWidth * scale);
    newFigHeight = floor(newFigHeight * scale);
else
    scale = 1;
end

figPosition(3) = newFigWidth;
figPosition(4) = newFigHeight;

% Translate figure position if necessary
deltaX = (screenSize(3) - figRightBorder) - (figPosition(1) + figPosition(3));
if (deltaX < 0)
    figPosition(1) = figPosition(1) + deltaX;
end
deltaY = (screenSize(4) - figTopBorder) - (figPosition(2) + figPosition(4));
if (deltaY < 0)
    figPosition(2) = figPosition(2) + deltaY;
end

% Change axes position to get exactly one pixel per image pixel.
% That is, as long as scale = 1.
dx = scale*imSize(2)/figPosition(3) - axPosition(3);
dy = scale*imSize(1)/figPosition(4) - axPosition(4);
axPosition = axPosition + [-dx/2 -dy/2 dx dy];

% OK, make the changes
set(axHandle, 'Position', axPosition);
set(figHandle, 'Position', figPosition);

% Restore the original units
set(axHandle, 'Units', axUnits);
set(figHandle, 'Units', figUnits);
set(0, 'Units', rootUnits);

% Warn if the display is not truesize, unless the truesize warning
% has been disabled according to a toolbox preference setting.
if (strcmp(iptgetpref('TruesizeWarning'), 'on'))
    if (scale < 1)
        message = ['Image is too big to fit on screen; ', ...
                    sprintf('displaying at %d%% scale.', round(100*scale))];
        warning(message);
    elseif (scale > 1)
        message = ['Image is too small for truesize figure scaling; ', ...
                    sprintf('\ndisplaying at %d%% scale.', round(100*scale))];
        warning(message);
    end
end

%--------------------------------------------
% Subfunction Resize3
%--------------------------------------------
function Resize3(axHandle, imHandle, imSize, colorbarHandle)
% Resize figure containing an image, a colorbar, 
% and nothing else.

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

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;

colorbarGap = 30;   % pixels
colorbarWidth = 20; % pixels

caxHandle = get(colorbarHandle, 'Parent');
figHandle = get(axHandle, 'Parent');
figUnits = get(figHandle, 'Units');
caxUnits = get(caxHandle, 'Units');
rootUnits = get(0, 'Units');
set(figHandle, 'Units', 'pixels');
set(caxHandle, 'Units', 'pixels');

caxPos = get(caxHandle, 'Position');
figPos = get(figHandle, 'Position');

if ((axPos(1) + axPos(3)) < caxPos(1))
    orientation = 'vertical';
else
    orientation = 'horizontal';
end

% Use MATLAB's default gutters
defFigPos = get(0,'FactoryFigurePosition');
defAxPos = get(0,'FactoryAxesPosition');
gutterLeft = round(defAxPos(1) * defFigPos(3));
gutterRight = round((1 - defAxPos(1) - defAxPos(3)) * defFigPos(3));
gutterBottom = round(defAxPos(2) * defFigPos(4));
gutterTop = round((1 - defAxPos(2) - defAxPos(4)) * defFigPos(4));

% 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;
while (~done)
    if (strcmp(orientation, 'vertical'))
        newFigWidth = imageWidth + gutterLeft + gutterRight + ...
                colorbarGap + colorbarWidth;
        newFigHeight = imageHeight + gutterBottom + gutterTop;
    else
        newFigWidth = imageWidth + gutterLeft + gutterRight;
        newFigHeight = imageHeight + gutterBottom + gutterTop + ...
                colorbarGap + colorbarWidth;
    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(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

axPos = [gutterLeft gutterBottom imageWidth imageHeight];

if (strcmp(orientation, 'vertical'))
    left = gutterLeft + imageWidth + colorbarGap;
    bottom = gutterBottom;
    width = colorbarWidth;
    height = imageHeight;
else
    left = gutterLeft;
    bottom = gutterBottom;
    width = imageWidth;
    height = colorbarWidth;
    axPos(2) = axPos(2) + colorbarGap + colorbarWidth;
end
caxPos = [left bottom width height];

set(figHandle, 'Position', figPos);
set(axHandle, 'Position', axPos);
set(caxHandle, 'Position', caxPos);

% Restore the units
drawnow;  % necessary to work around HG bug      -SLE
set(figHandle, 'Units', figUnits);
set(axHandle, 'Units', axUnits);
set(caxHandle, 'Units', caxUnits);
set(0, 'Units', rootUnits);

% Set the nextplot property of the figure so that the
% axes object gets deleted and replaced for the next plot.
% That way, the new plot gets drawn in the default position.
set(figHandle, 'NextPlot', 'replacechildren');

% Warn if the display is not truesize, unless the truesize warning
% has been disabled according to a toolbox preference setting.
if ((scale < 100) & strcmp(iptgetpref('TruesizeWarning'), 'on'))
    message = ['Image is too big to fit on screen; ', ...
                sprintf('displaying at %d%% scale.', floor(scale))];
    warning(message);
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?