Draw the path of a random movement of a mouse using drawnow

2 views (last 30 days)
I have created a function where mouse moves randomly inside a maze until it find the exit. The function takes image of a maze as input and randomly moves inside it until it find the exit. Now I want to track the path of the mice movement at realtime using drawnow function. The current position of mouse should be shown as a red, filled circle, and the frequency with which he visited particular indices should be shown in inverted bone color scale to represent the number of times mouse has visited a particular index. A example is shown in the image.
The movement of the mice code works fine and mice is able to solve the maze, what I need is to plot this the path. I am not sure how to use the drawnow function in this case. How can I use the darwnow function to update the postion of the mouse and also to update the path with the number of frequencies it visited an index?

Accepted Answer

Walter Roberson
Walter Roberson on 15 Nov 2022
drawnow() is a "show me what you have so-far" function.
It is inefficient to have the screen update after every graphics change -- and if you tried to do that you would end up with some oddities. For example if you are moving an object from one place to another, and you update after every change, then would you want a copy of the object to appear in the new place as well before being removed from the old place, or would you want the object to temporarily be in no-place because you removed it from view before making it visibile in the new location?
So you do not want to update the screen after every change.
If you do not update the screen until after all changes have been done, then you would not be able to do animation.
So when should the screen update? Well, MATLAB defines that the screen updates:
  • when you pause(), the graphics system will finalize any internal updates and update the screen
  • likewise when you call figure() -- regardless of whether you are creating a new figure or asking an existing figure to become the "current" figure, calling figure() triggers a screen update
  • waitfor() or uiwait() trigger screen updates
  • returning to the command line (either end of execution or because of keyboard() or for debugging purposes) triggers a screen update
  • drawnow() also triggers a screen update, except if you used "drawnow limitrate", in which case
If it has been less than 50 milliseconds since
the last update, or if the graphics renderer is busy with the previous
change, then the new updates are discarded.
So do not think in terms of drawnow somehow taking action itself: just call drawnow() at each point that you want the screen to update.
I think your loop might look like
  1. generate new coordinates
  2. update an array of counters to increase the count at the (rounded) coordinates. Update the CData of an image() with the new array
  3. update the XData and YData associated with the cursor dot
  4. drawnow()
  1 Comment
Nammuni Arachchilage Chathura Jinath Dinelka Sen
Thanks for the very descriptive answer. I was able to get it done with set() and drawnow functions placed inside a loop.

Sign in to comment.

More Answers (0)


Find more on Desktop 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!

Translated by