Placing picture on axis of MATLAB figure

こ雲淡風輕ζ 提交于 2019-12-08 03:38:17

问题


I want to make animation of ball (given by the picture here) which starts from origin and goes through a track given by x-vector, y-vector, z-vector (each of nX1).I know I need to use the getframe command but I don't know how to move the picture on the axis. I know that I can put a picture in one of the corner by defining new axis, e.g (exmaple taken from MATLAB offical forum):

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);

but since I don't want to create seperate axis, this does not help. How can I define movement of my ball on a given axis?


回答1:


You can move the object by storing the handle returned by the image drawing function and setting its 'XData', 'YData', and 'ZData' properties. Here is a little example; this example uses warp to draw the image on a spherical surface (generated using sphere), and then moves it around a random path.

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


来源:https://stackoverflow.com/questions/16773127/placing-picture-on-axis-of-matlab-figure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!