How can I display an image on a point of the plot?

조회 수: 25 (최근 30일)
Riccardo Basile
Riccardo Basile 2019년 1월 14일
댓글: Riccardo Basile 2019년 1월 17일
I have an animated plot obtained using a for cycle and drawnow. I'de like to display a small image on the last displayed point of the plot at every interation. The strange thing about this problem is that I managed to get it work in one function but it doesn't work in two other functions I made. The code that works is the following:
function [] = GroundTrackGFX(latitude, glongitude, date, Nt, inc, omegarot,...
dt, lplon, lplat)
gtf = figure('position', [25 200 800 500], 'NumberTitle', 'off',...
'Name', 'Ground track');
M = imread('Earth texture.png');
S = imread('Satellite icon.png');
imagesc('XData', [-180 180],'YData', [90 -90],'CData', M);
daspect([1 1 1]); hold on
xlabel('Longitude [deg]'); ylabel('Latitude [deg]');
axis([-180 180 -90 90]);
ax = gca; ax.TickDir = 'out';
plot(glongitude(2), latitude(2), 'g.', 'markersize', 16); hold on
title(date(2));
I = imagesc('XData', [glongitude(2)-3 glongitude(2)+3], 'YData',...
[latitude(2)-3 latitude(2)+3], 'CData', S);
for i = 3 : Nt
title(date(i));
delete(I);
I = imagesc('XData', [glongitude(i)-3 glongitude(i)+3],...
'YData', [latitude(i) - 3 latitude(i) + 3], 'CData', S);
% other stuff (plots)
drawnow
end
% more stuff
end
One that doesn't work (the animated plot works fine but S is not displayed) is this:
function [] = MGroundTrackGFX(xm, ym, RT, date, Nt, glongitude, latitude)
gtf = figure('position', [25 200 800 500], 'NumberTitle', 'off',...
'Name', 'Ground track');
M = imread('mollweide.png');
S = imread('Satellite icon.png');
plot(xm(2), ym(2), 'g.', 'markersize', 16); hold on
title(date(2));
I = imagesc('XData', [xm(2)-3 xm(2)+3], 'YData',...
[ym(2)-3 ym(2)+3], 'CData', S);
for i = 3 : Nt
title(date(i));
delete(I);
I = imagesc('XData', [xm(i)-3 xm(i)+3], 'YData',...
[ym(i)-3 ym(i)+3], 'CData', S);
% other stuff (plots)
drawnow
end
% more stuff
end
I really can't understand why the first works and not the second... To me they're pretty much identical. Can anyone tell me why this is the case and how I can do to solve the problem?

채택된 답변

Rik
Rik 2019년 1월 15일
Instead of deleting and recreating those graphics every iteration, you could also update the XData and YData properties of the image object that is returned by imagesc. That should solve your problem, as well as speeding up your code a bit.
  댓글 수: 3
Rik
Rik 2019년 1월 16일
Here is a MWE:
figure(1),clf(1)
ax=image;%trigger default image
axis([0 100 0 100])
for n=1:5
Xoffset=randi(36);
Yoffset=randi(36);
ax.XData=[1 64]+Xoffset;
ax.YData=[1 64]+Yoffset;
drawnow
pause(0.5)
end
Riccardo Basile
Riccardo Basile 2019년 1월 17일
Thanks a lot it works great!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2019년 1월 16일
Is this kind of what you're looking for? To display an image on a graph?
% Draw a small image inset in the upper right corner of a larger plot.
% Ref: https://www.mathworks.com/matlabcentral/answers/60376-how-to-make-an-inset-of-matlab-figure-inside-the-figure#comment_654093
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
x = linspace(0, 1);
y1 = sin(2*pi*x);
figure(1)
% plot on large axes
plot(x, y1, 'LineWidth', 2)
grid on;
ax1 = gca; % Store handle to axes 1.
% Create smaller axes in top right, and plot on it
% Store handle to axes 2 in ax2.
ax2 = axes('Position',[.6 .6 .3 .3])
box on;
fileName = 'peppers.png';
rgbImage = imread(fileName);
imshow(rgbImage);
axis('on', 'image');
% Now draw something back on axis 1
hold(ax1, 'on'); % Don't blow away existing curve.
y2 = cos(2*pi*x/0.7);
plot(ax1, x, y2, 'r-', 'LineWidth', 2);
0000 Screenshot.png
Adapt as needed.
  댓글 수: 1
Riccardo Basile
Riccardo Basile 2019년 1월 17일
Thanks for the answer! However Rik's answer was the one I was looking for

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by