A moving point plot in MATLAB figure with animation between 2 point
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Hi everybody; i want to make a moving point animation between a = [41.50,-70.95] b = [ 41.80 , -70.5].How can i do this?
Accepted Answer
Geoff Hayes
on 29 Dec 2014
Farhad - what kind of animation do you want? Should the point follow a straight line, or do something else? If the former, then try the following
a = [41.50, -70.95];
b = [ 41.80 , -70.50];
% straight line function from a to b
func = @(x)a(2) + (a(2)-b(2))/(a(1)-b(1))*(x-a(1));
% determine the x values
x = linspace(a(1),b(1),50);
% determine the y values
y = func(x);
% create the figure
figure;
% get a handle to a plot graphics object
hPlot = plot(NaN,NaN,'ro');
% set the axes limits
xlim([min(a(1),b(1)) max(a(1),b(1))]);
ylim([min(a(2),b(2)) max(a(2),b(2))]);
% iterate through each point on line
for k=1:length(x)
% update the plot graphics object with the next position
set(hPlot,'XData',x(k),'YData',y(k));
% pause for 0.5 seconds
pause(0.5);
end
So on each iteration of the for loop, we just replace the (x,y) coordinate of the last drawn point with the new one and then wait for half a second before drawing the next point, which creates a kind of animation.
7 Comments
Farhad
on 29 Dec 2014
First of all thanks for your code,it is exactly what i want,but i must show location data in another window(think i must use sprintf),and this location must be deponds in time,have you any idea about this?
Geoff Hayes
on 29 Dec 2014
By another window, do you mean a static text box? Do you want to design a GUI with an axes object and a text control? if so, start with creating the GUI using GUIDE...
Farhad
on 29 Dec 2014
ok thanks :)
Farhad
on 29 Dec 2014
Edited: Geoff Hayes
on 30 Dec 2014
soryy Geoff Hayes,if i want show the point coordinats near the red point,what should i do?
hTxtcoords= text(y(1,1), y(1,2), sprintf('(%.2f,%.2f)',y(1,:)),'Color',[0.2 0.2 0.2], 'FontSize',8,'HorizontalAlignment','left', 'VerticalAlignment','top');
set(hTxtcoords, 'Position',x(k,:), 'String',sprintf('(%.3f,%.3f)',y(k,:)))
i use those code but,cant work. have you any idea?
Geoff Hayes
on 30 Dec 2014
Farhad - i suspect that the problem that you are having is in the use of the x and y coordinates. At each iteration of the for loop, you will want to place the text box at the position x(k),y(k) and use that same position in the sprintf. Like with the plot, create (outside of the for loop) the handle to the text object as
hTxtcoords = text(NaN,NaN,'','FontSize',8,'HorizontalAlignment','left', 'VerticalAlignment','top');
and then within the for loop, add the following line after the plot "update" and before the pause
set(hTxtcoords, 'Position',[x(k) y(k)], 'String', ...
sprintf('(%.3f,%.3f)',x(k),y(k)));
Try the above and see what happens!
Farhad
on 30 Dec 2014
Thank you Geof,i will say my regards to you in presentation.
passioncoding
on 3 Jan 2019
Can I ask same question related to this kind a animation? plz help me
More Answers (0)
Categories
Find more on Graphics Object Properties in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)