# How do I change the axis things get plotted on?

1 ビュー (過去 30 日間)
Nick Woolsey 2021 年 4 月 23 日

I'm new to matlab and I decided to take on an ambitous project for school. I'm trying to make a reletively simple game but I'm running into a problem where text and graphs will be plotted on images that I plotted before. Below is a small snipet of my code showing the problem. The idea is to shoot a laser from the ufo but instead it graphs it on the ufo. I'd like to know how to fix this and do the same for text. Thanks!!
clc, clear, close all
figure
hold on
axis([0 8 0 8])
SpaceBackground = image(xlim,ylim,Image);
axis off
UFO = axes('position',[0.13,0.1115,0.19,0.2]);
imagesc(UImage);
axis off
x = linspace(0,8);
y = 1;
pause(3)
for i = 1:8
p = plot(x(i),y,'ro');
pause(0.1)
delete(p);
end

サインインしてコメントする。

### 採用された回答

Geoff Hayes 2021 年 5 月 4 日
Nick - you need to specify the axes as described at create line on axes. In your case, you might want to save the axes handle to the first axes after you have drawn the image
SpaceBackground = image(xlim,ylim,Image);
spaceBgrdAxes = gca;
Then you would plot on this axes lke
for i = 1:8
p = plot(spaceBgrdAxes, x(i),y,'ro');
pause(0.1)
delete(p);
end
using a hold on where appropriate.
But is this really what you want to do? While the "laser" would appear on the appropriate axes, it may appear behind the second one (depending upon how the second axes is placed over top of the first). You might need to plot it relative to the position of the UFO on the space background plot. Or maybe you want to combine the space background and UFO into a single image and then update the image with a drawing for the laser rather than creating a new plot graphics object. A crude attempt at this might be
function imageUpdateExample
close all;
% create a black background or load an image
spaceBackground = randi(1,100,300,3) - 1;
hBackground = image(spaceBackground);
axis equal;
% create a yellow UFO or load an image
ufo = randi(1,25,25,3) * 255;
ufo(:,:,3) = 0;
% place the UFO in top left corner (for convenience)
hBackground.CData(1:size(ufo,1),1:size(ufo,2),:) = ufo;
% fire the laser
for k = 1:100
fireLaser(k);
pause(0.1);
end
function fireLaser(k)
% update four pixels for laser (triangle shape)
row = ceil(size(ufo,1)/2);
col = size(ufo,2);
hBackground.CData(row, col + k, :) = [255 0 0];
hBackground.CData(row, col + k + 1, :) = [255 0 0];
hBackground.CData(row - 1, col + k, :) = [255 0 0];
hBackground.CData(row + 1, col + k, :) = [255 0 0];
% restore background for previous laser to simulate movement
if k > 1
hBackground.CData(row, col + k - 1, :) = spaceBackground(row, col + k - 1, :);
hBackground.CData(row, col + k + 1 - 1,:) = spaceBackground(row, col + k - 1 + 1, :);
hBackground.CData(row - 1, col + k - 1,:) = spaceBackground(row - 1, col + k - 1, :);
hBackground.CData(row + 1, col + k - 1,:) = spaceBackground(row + 1, col + k - 1, :);
end
end
end

サインインしてコメントする。

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by