using a vector/array as a counter in a for loop, or while loop?!

7 views (last 30 days)
Hello All,
At the moment a program that I am working on generates random arrays (not random, but different each time). As an example, my A array:
A=[1.000,1.0062,.9831,.3098,-.1082,2.098,...]
My goad is to draw a square that moves with respect to this array, so I have drawn a 1x1 square in red, pause 1 second, then draw over it in white. But what I cannot seem to figure out is I want the next square to still be a 1x1, but i want it to move over the difference between entry 2 & 1 in my A array, and the 1x1 square drawn after that I want to be the difference between entry 3 & 2 in my A array. Is this possible? I tried to make the A array my counter so it would look something like:
y=1:A
for k=1:A
plot([y(k),y(k+1),y(k+1),y(k),y(k)],[0,2,0,2,0],'r')
pause(0.2)
plot([y(k),y(k+1),y(k+1),y(k),y(k)],[0,2,0,2,0],'w')
end
But this does not work. Any ideas on how I can achieve this? I need the motion of my block to mimic the values in my Array which are different each time!
Thanks

Answers (1)

Steven Lord
Steven Lord on 16 May 2019
Rather than creating and deleting the line each time, why not use one of the animation tools and/or techniques shown on this documentation page? In particular, from your description it sounds like you may be able to adapt the example on the "Animate Graphics Object" page to your needs.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!