add a moving image over a plotted trajectory

x = 0:pi/100:2*pi;
y = sin(x);
plot(x, y);
%i want my image to move in this way
alpaca = 'alpaca.png';
[I,map,transparency] = imread(alpaca);
%i have this code to remove the background of my image
%i tried using image sc but i couldnt get it to work
%thank you!

 Accepted Answer

DGM
DGM on 7 May 2022
Edited: DGM on 8 May 2022
Assuming you actually want it to move in real time, this should be a start
x = 0:pi/100:2*pi;
y = sin(x);
plot(x, y); hold on
%i want my image to move in this way
fname = 'alpaca.png';
[inpict,~,alpha] = imread(fname);
inpict = flipud(inpict); % necessary to keep image upright
alpha = flipud(alpha);
imgsize = [0.7 0.8]; % [x y] in plot coordinates
% get current coordinates for the image
xx = [-0.5 0.5]*imgsize(1) + x(1);
yy = [-0.5 0.5]*imgsize(2) + y(1);
hi = image(xx,yy,inpict);
hi.AlphaData = alpha; % set alpha
% enforce axes extents
axis equal
xlim([0 2*pi] + [-0.5 0.5]*imgsize(1))
ylim([-1 1] + [-0.5 0.5]*imgsize(2))
for k = 1:numel(x)
hi.XData = [-0.5 0.5]*imgsize(1) + x(k);
hi.YData = [-0.5 0.5]*imgsize(2) + y(k);
% wait
pause(0.02)
end

7 Comments

Hi,
i have copied your code but the image does not display,
do you khnow the reason please?
thank you for your help
DGM
DGM on 1 Jul 2022
Edited: DGM on 1 Jul 2022
What version are you using?
Are you using the same image?
Is the code exactly the same, or have you made changes?
Are there any error messages?
If you changed the curve, you'll probably have to also change the xlim and ylim lines accordingly, otherwise the actual plot content will be out of frame.
Can you help me please, i want to do this: A rectangle will appear on the screen. the user must click on the rectangle as quickly as possible for 60-70 seconds. and the rectangle(or any different image) appears in mouvement on the screen, when it appears(each time in different place), the user must click with the mouse and it's validated if he doesn't click it not valid
Is the rectangle following a predetermined path, or is it appearing in random locations? The latter case is likely less complicated to implement.
Either way, it's probably best to open a new question for this. If you're intending to do this with Psychtoolbox, make sure to mention your intent.
Thank you for your answer, yes it can be random locations
This is probably the best demonstration of a sprite being used in Matlab so far.
Is it possible to make a sprite like this which the user can drag to different areas of the grid?
Probably. I'm not really sure of what the best way to do that would be. It may require setting up some interaction callbacks. I don't think I've ever tried to do this sort of thing specifically. I generally avoid interactive graphics as much as I can.

Sign in to comment.

More Answers (1)

Something like this?
x = 0:pi/100:2*pi;
y = sin(x);
figure();
plot(x, y);
hold on
xlim([0 2*pi]);
ylim([-1 1]);
I = imread('alpaca.png');
im = image(I,'XData',[0 1],'YData',[0 1]);
for ii = 1:numel(x)
set(im,'XData',x(ii)+[-0.5 0.5],'YData',y(ii)+[-0.5 0.5]);
drawnow();
end

5 Comments

hi, the same problem, the image does not display when i copied your code
did you khow why please?
Do you have the image file alpaca.png?
ss
ss on 5 Jul 2022
Edited: ss on 5 Jul 2022
Hi, last time it worked bu now it gives me this error
Subscript indices must either be real positive integers or logicals
Error in mouvement_sinusoidal (line 14)
im = image(I,'XData',[0 1],'YData',[0 1]);
Try doing
clear image
and then running it again.
That is, you may have a variable called image in your workspace that needs to be cleared before you can use the image function.
yes it works! thank you a lot

Sign in to comment.

Products

Release

R2021b

Tags

Asked:

on 7 May 2022

Commented:

DGM
on 7 Apr 2025

Community Treasure Hunt

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

Start Hunting!