首页 > 解决方案 > 多图 Matlab 包括图像

问题描述

我使用该tiledlayout(2,2)命令在 Matlab 中创建绘图。前三个标题字段填充了正常的地块。第四个也是最后一个字段是带有图像的字段。我使用了命令IMG = 'mypic.tif'imshow(IMG)。我想知道为什么我不能更改图像的大小。这不能用tiledlayout命令吗?我已经查阅了关于tiledlayoutand的纪录片imshow(),但没有发现任何对我有帮助的东西。

标签: matlabmatlab-figure

解决方案


确实似乎使用该功能是不可能的tiledlayout。但是,可以使用该subplot功能。下面,我将举例说明这是如何工作的。

考虑以下脚本:

% Generate some dummy data for the four plots
x = linspace(0, 2*pi, 25);
y = sin(x);

% Load sample image
image = imread('ngc6543a.jpg');

% Generate the figure
fig = figure(1);

% The three subplots
for i = 1:3
    subplot(2,2,i);
    plot(x, y);
end

% The image
subplot(2,2,4);
imshow(image);

% Increase size of the image
size_factor = 1.4; % 1.0 is original size

im_ax = fig.Children(1);    % axes belonging to image
dims = im_ax.Position(3:4); % current dimensions

dims_new = size_factor * dims;  % scale old dimensions
dxdy = (dims_new - dims) / 2;   % offset for left bottom corner of image

im_ax.Position = [im_ax.Position(1:2) - dxdy, dims_new]; % update position

在此脚本中,我们首先为三个“正常”图生成一些虚拟数据并加载示例图像(此图像随我的MATLAB安装一起提供)。随后,我们创建一个图形。for创建图形后,我使用循环和subplot功能将三个虚拟图添加到图形中。然后,我们使用imshow第四个子图绘制图像。

现在,有趣的部分开始了。首先,我为图像定义了一个比例因子 ( size_factor)。然后,我检索绘制图像的轴并将其存储在im_ax. 从这个轴,我检索该Position字段的最后两个元素并将它们存储在dims. 这两个元素定义了轴的大小。根据 的值size_factor,我计算轴的新大小并将其存储在dims_new. 为确保轴(以及图像)保持居中,我们需要计算需要将轴的左下角移动多少(其坐标存储在Position字段的前两个元素中)。此结果存储在dxdy. 我们要做的最后一件事是简单地更新Position绘制图像的轴的字段。

现在,向您展示一些结果:

  1. size_factor等于1.0size_factor 等于 1
  2. size_factor等于1.4size_factor 等于 1.4
  3. size_factor等于0.55size_factor 等于 0.55

推荐阅读