将图片放在MATLAB图的轴上

时间:2013-05-27 12:22:13

标签: matlab matlab-figure

我想制作球的动画(由图片here给出)从原点开始并经过由x-vector,y-vector,z-vector(每个nX1)给出的轨迹.I知道我需要使用getframe命令,但我不知道如何在轴上移动图片。我知道我可以通过定义新轴在一个角落放置一张图片,例如(例如,摘自MATLAB官方论坛):

numberOfDataPoints = 200;
sampleData = 100*rand(1,numberOfDataPoints);
plot(sampleData);
xlim([1, numberOfDataPoints]);
hold on;
plot(sampleData);
xlim([1, numberOfDataPoints]);
axes1Position = get(gca, 'Position');
logoSizeX = 0.1;
logoSizeY = 0.1;
% Position the logo in the upper right.
x1 = axes1Position(1) + axes1Position(3) - logoSizeX;
y1 = axes1Position(2) + axes1Position(4) - logoSizeY;
hAxis2 = axes('Position', [x1 y1 logoSizeX logoSizeY]);
axis off;
imshow(ball.jpeg);

但由于我不想创建单独的轴,这没有用。如何定义我的球在给定轴上的移动?

1 个答案:

答案 0 :(得分:2)

您可以通过存储图像绘制功能返回的手柄并设置其'XData''YData''ZData'属性来移动对象。这是一个小例子;此示例使用warp在球面上绘制图像(使用sphere生成),然后围绕随机路径移动。

close all;

% Load image
[img, imgMap] = imread('peppers.png');

sphereImgSize = min(size(img, 1), size(img, 2)); 
sphereImg = img(1:sphereImgSize, 1:sphereImgSize, :);

% Generate sphere vertices
[X, Y, Z] = sphere(sphereImgSize);

lims = [-10 10];

figure;
axes;

hImg = warp(X, Y, Z, sphereImg); % NOTE: Store handle returned

xlim(lims);
ylim(lims);
zlim(lims);
axis square;

% Set up movement path
nFrames = 100;
randPathFun = @()rand(nFrames, 1) * diff(lims) + lims(1);
pathX = randPathFun();
pathY = randPathFun();
pathZ = randPathFun();

% Move the image by setting its 'XData' and 'YData' properties
for ii = 1:nFrames
    xData = X + pathX(ii);
    yData = Y + pathY(ii);
    zData = Z + pathZ(ii);
    set(hImg, 'XData', xData, 'YData', yData, 'ZData', zData);
    pause(0.1);
end