How can I include visuals in this simulation?
4 views (last 30 days)
Show older comments
So the attached files include code for a traffic simulation. How can I make this visual? I want something like a traffic signal on a road and instead of the stars that are in this programs output I want to include image like some sort of clip art. So basically a road with the cars appearing and disappearing. How can I make this work? If you can include some helpful tutorials or links Id be grateful.
0 Comments
Answers (1)
Madhav Rajan
on 5 Aug 2015
I understand that you want to visualize the traffic with some clip art images. A sample function "draw.m" is attached which takes the number of cars as input and places that many cars on the road. The script uses an image for the road and an image for the car and calls the "image" and "imshow" functions to render the images. To update the visualization at each timestep, make a call to the "draw" function that is provided and pass the argument 'CARS' before ending the for loop in the 'traffic.m' file.
You can extend this function to create images for your signals and manipulate them if they are red and green.
I have also attached the sample road and car images that I have used in the sample 'draw' function. You can refer the following link for more information on "drawnow" and how to use it for animation:
Hope this helps.
5 Comments
Walter Roberson
on 10 Jul 2017
If the imshow() is giving an error then you might be using an older version of MATLAB in which imshow() was part of the Image Processing Toolbox; in that case, change imshow() to image() as Madhav Rajan indicated.
Another possibility is that you might not have road.jpg available in the current directory or a directory that is on your MATLAB path.
The code given relies upon R2014b or later. If you are using an earlier MATLAB then it would need some modification; please indicate the target version of MATLAB in that case.
See Also
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!