Creating Movies and Animations in Matlab

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to another video today I'd like to talk about making movies and animations in MATLAB as you know Malaba is a great tool for visualizing data and several of our previous videos have talked about generating static 2d and 3d plots in MATLAB if you haven't had a chance to view those I would recommend taking a minute to step back and go through those tutorials first as we're going to be building off some of the content we discussed in them I'll leave a link for those videos in the description below that being said why don't we take a minute and briefly talk about animations and movies from a theoretical standpoint before jumping into MATLAB as I'm sure you know any movie or animation is just a series of still images shown one after another if you show these images together in rapid enough succession the brain interprets this as fluid continuous motion so for example I've made myself this this crude flip book to illustrate this fact you're gonna have to excuse my horrible drawing I'm not not much of an artist in fact here's a picture that my daughter recently just did in kindergarten you know if you look at these two I kind of think she did a better job than I did my only saving grace is that she told me that this was supposed to be a giraffe but but anyway that's sort of beside the point here so um if I flip through this book here you'll basically you can see a movie of what I was trying to illustrate here so I'm trying to have a make a movie this is supposed to be a dog catching a biscuit in his mouth and then wagging his tail here yeah so again if you flip through this a couple times you'll get the idea here that I'm just displaying a whole bunch of still images one at a time here and it sort of looks like animation here right so oh hello what are you doing here oh I see you're looking for this okay so here no sit sit sit sit okay let's try this okay let's try throat oh wait ah well okay that was a real thing I don't know did it look anything like my flip book here catch the biscuit and then wag the tail I don't know okay anyway um let's go ahead and take another step with this idea all right so what we saw here was that animation in MATLAB really follows a workflow which is similar to that of creating a flipbook so the first thing that we want to do here is go ahead and you know you could run a simulation or otherwise generate data right that's the first thing you want to do here is just figure out what is the data that's going to drive this simulation or this animation here right so the next thing that you're gonna want to do here is once you have all that data the second step here is well we need to start building our flipbook here so I just need to draw or render the scenario and make one page of my flip book here so step two is to you know draw a slash render the scenario at some time maybe let's call it TK right once you have that we saw that all right I've got the the one single page in my flip book done here the next thing I want to do here is I want to start saving this year so I'm gonna want to go ahead and take a snapshot of the scenario so I'm gonna save my single page of my flip book once I've got that here I just want to basically repeat here right so the next thing here is I want to go ahead and then an advanced time - TK plus 1 right and then we're just gonna basically go back and repeat step two and we're gonna keep going through building one frame or one page in my flip book one at a time and saving it to the larger flip book here right which was where we were gonna take that snapshot once I've done this and drawn all of my frames of animation that what I can lastly do here is go ahead and save the movie okay so at a high level this is the the outline or the way that we're going to go about tackling this so what I'd like to chat about now is maybe a couple of MATLAB functions that might be helpful at each one of these steps here so let's start your step one this is pretty easy here right generating the data here or running some kind of simulate so maybe you've got some fancy flight simulator or work which will run a scenario and kick out all of this data and save it to a file here so all you need to do here is just basically load that data in right so load is probably going to be helpful here if you're familiar with Simulink here you know that you can run a Simulink model from a MATLAB script to generate data here this is very helpful you could use the sim command to do that again we've got a video here discussing how to interact between a MATLAB script and a simulation model here I'll leave a link for that in the description as well here but again step one is just about getting the data that you are trying to animate into your MATLAB workspace here so once you've done that here how about drawing or rendering the scenario at a given time well you see this is really just talking about plotting right all you're doing is plotting or drawing one single frame of your animation here so just use any of the plot tools that you're already comfortable with right plot plot 3 surf any of these other plotting commands that we talked about you're basically just trying to make one page your flip book here a couple of things that might be helpful as you can probably imagine is you're trying to generate a complicated animation or a complicated scenario you're probably going to want to put the hold on right that way you can draw multiple plots on the same figure one thing that we're going to see later when we go over to the computer those if you think about this long enough because this plotting command is jammed inside this for loop here you're gonna want to have to periodically wipe the slate clean right every time you come through this you want to be drawing on a new blank sheet of paper here or a blank figure so you're probably going to want to use CLF which is for clear the figure here and again we'll take a look at exactly how to implement that here in a second I just want to put some of these commands up here so we can see all right step 3 take a snapshot of the scenario once you've drawn your one page of the flip book we want to grab that frame and save it into the larger flip book here right so malov has a great function for doing that it's called get frame okay um and then we're gonna go ahead and advance time like we said if you put this inside a for loop or a while loop that this step is automatically gonna get handled for you here although maybe we should point out here that if your data that you're simulating here is very tempura tightly spaced here you might have tons and tons and tons of data thousands of points of data here right you might not want to draw every single plot here of every single point because that's going to be a very dense movie so here you might want to think about implementing some logic that maybe skips every other or every 10th frame or every only draws every tenth frame here so you don't have such a large movie here so I'll let you think about how to implement that logic here some functions that might be helpful MATLAB has a continued operation that might allow you to skip parts of a loop if you're not interested and then finally coming down here to saving the movie here so to save the movie here you're gonna use the video writer class as well as we're going to probably end up having to use the right video function okay so again that's the outline of our workflow as well as some commands here that are going to help us put this together into a cohesive functioning script here Before we jump over to MATLAB here maybe let's talk about a an example that we might want to animate here so let's take a look at how about trying to animate a trajectory of a point here so let's say I've gone ahead and run some simulation or I have some thing which generates the trajectory of some point in space here so the position vector that describes where this point is located at any given x y&z location at any given time T here let's say this is something like I don't know five cosine of T - sine of T and T here right and this is for the ranges of T and the range of 0 to 2pi ok so this is how my particle is going to move through space here and if you look at this long enough you see that what this basically describes is the particle moving in this upward elliptical single helix right so if we wanted to sketch out what we think this should look like let's just briefly draw this here so you got X Y & Z here I think when time T equals zero maples news in red here right when time T equals zero the particle starts out here at the point five zero zero right and then as T goes up to 2 pi we see that it spirals up in an ellipse with semi-major axis 5 and semi-minor axis too and basically comes up like again I think well I'm not I'm not the best artist I think we evidence that by looking at my flip book here but at the end of the day I think where this particle ends up here is at 5 0 to PI right so um what we would like to maybe animate here or demonstrate here is why don't we try to draw this trajectory but then it would be awesome if we've had AB dot or something of that representing the particle and then this particle would be able to move up this trajectory in time and then eventually end up at the ending location so that would be really neat if we could see that actually happen in MATLAB so if if my flip book didn't give you nightmares and you still want to continue on why don't we jump over to the computer and take a look at how we're gonna implement this in MATLAB script so let's go ahead and get started so let's just go ahead and say that we're trying to illustrate animation and movies in MATLAB so let's go and do our normal clear CLC close all and now all I need to do is just walk through our five step process here right so we said step one here was generate data here and we said for our example we said time was gonna go from zero to two pi and let's use a hundred points to do that and the X points were gonna be five times cosine of T I believe Y was two times sine of T and Z was just gonna be T alright so there's step one again if you had a more complicated scenario like having to load in information from a external source or run a Simulink model view that's where you could easily do that but now once we have all of the data step two is basically just draw a slash render the scenario right so let's go and start ourselves a new figure so we can draw on something here and now I'm gonna start a for loop from K gets 1/2 length of T and then what I need to do now is in here is first let's just go ahead and extract the data at the current time so in other words I want to get T sub K being T sub K and I'm gonna make another variable called XK which is gonna be X of K and YK is gonna be Y sub K and ZK is gonna be easy okay okay so there we go that's the current location of this particle here so why don't I go ahead and plot the current location of the particle okay so I'm just gonna go ahead and use my usual plot 3 and I'll just plot X K Y KZ K and let's make this a green circle here that is a little bit larger so I'll make the line width 3 here and I'll make the marker side is maybe 15 just so it shows up a little bit more and now what I also want to do here is I want to draw more stuff like for example I want to draw the curve here or the entire trajectory here so let's go say plot the entire trajectory right so what I probably need to do here is since I'm plotting multiple things on the same figure let's turn the hold on okay and then we will go ahead and say plot 3 of the entire trajectory right XY Z here let's make it a black line here and we'll maybe make it a little bit thicker here okay great all right there we are and what we can do is we can we can continue decorating the plot if we don't want so for example we could go ahead and save things like we can turn the grid on let's make the X label say X the Y labels Y the Z label say Z and then maybe the title of the plot can be how about the particle at time equals let's make this let me wrap this thing in an entire string here on num2 string of TK seconds there we go okay and maybe let's also let's set the view point I think we talked about the the view here so I'm specifying azimuth and elevation so I can rotate the plot to be at a very specific location okay so this looks pretty reasonable why don't we go ahead and save this script and I'm just gonna put this here in some temporary location here maybe I'll call this thing animation and the nation demo okay and um let's go ahead and try to run this here and what I would love to see here is a circle moving up a trajectory or this curve here but it's actually not gonna work so for example if we run this thing we're gonna render a couple of things that we might notice oops oh sorry I this should be line with not just with sorry I'm gonna try that again there we go okay ah alright that sort of didn't work so one thing that we notice here is it plotted all of the points here so we notice here that because we had this hold on it we just kept plodding and plodding and plodding and plodding one of the things that we probably need to do here is let's make a change here let's go ahead and wipe the slate clean so we are plotting with a blank figure so I'm gonna go ahead and put a seal F at the very top at the beginning of the loop here so when this comes in through its going to clear the figure here and we'll just draw only what we want to draw on each page so if I do this again this didn't seem to quite work if you notice here it just ended up drawing the very last image here I want to have seen this particle moving up and up and up the spiral the reason why it did this here is because MATLAB actually by default here is if you look at this you notice that our plotting commands all of this stuff is buried inside of a for loop malov is actually kind of a kind of smart I guess in this scenario and realizes that it is gonna slow way way way way way down if it has to draw every single image here in this in this for loop so it actually suppresses the plotting until you drop out of the for loop and then it will basically render the very last scene here for you so that's not really the behavior that we want here I really want to maybe just check to see if this animation is working properly so what I want to do here is I want to forced MATLAB to draw the image at this point here so what I can do here is Madeline has this cool a function called draw now here so if you just put a draw now in here it's going to force MATLAB to flush the graphics and plot this as it go so if I do this now now this is looking reasonable right you see this thing moving up and yeah our trajectory is going and Malaba is actually going to draw it as fast as it can here and when you call the drawing up now the other option here is let me comment that out and just show you another way to do this you can also use the pause command pause if you give it a number in here well basically pause the script for however many seconds and when it hits that pause it will also flush the graphics so you can see that if I run this now it runs at a little bit slower chunkier rate because every point two seconds it's waiting here and drawing the graphics so again that's another way you can force it to animate the scenario as it goes along so this looks great here but actually all we're doing right now is watching the flipbook occur one time on our screen right so this isn't exactly what we would like to do here because really we want to be able to save the flip book so let me go ahead and comment that out as well and really what we would like to do is in fact call the function called get frame so get frame what it's gonna do here is get frame acts like draw now in the sense that it's going to force the graphics to render but it will also return basically a bitmap or a matrix of all of the values of the current figure here so in other words it will grab what the current figure looks like and return that as an image object that you can say in other words it will grab one page of my flip book so what I can do here is I'll make a variable maybe I'm gonna call it movie vector it's okay gets get frame so movie vector is effectively going to be our flip book maybe maybe a better variable name should have been flip book here but what you can see is going on here when I run this every time it hits line 43 it's going to grab the current picture and jam it into this thing called movie vector so by the time this loop ends we should have an enormous thing called movie vector so let's come back over here and come to the MATLAB workspace and you can see yes that actually is the case here right you see got this one by a hundred Strutt here in fact I think if we look at the number of bytes this thing takes up we'll probably get a better idea of what this thing looks like so as you can see here yes movie vector is this enormous thing which is basically 100 bitmaps all saved together here I think it saves it as some other object if you're really curious yeah it's a strut with these things yeah it's got a dot see data field and a dot color map field but you can see it's basically a red green blue image here of 500 by 652 pixels here okay so at this point we've basically done steps 1 2 & 3 here so what are we on to now I think it's step 4 was what advanced time well that's pretty much happens automatically at the end of this for loop so we really don't need to do anything here right do not need to do anything all right so now let's go ahead and drop out of that for loop and then what we want to do here is step 5 here where we want to save the movie okay so now I've got this movie vector which is all of my flip books I really would like to print this out um as an actual avi or an mp4 file here right so the way to do that here is I'm gonna call video writer so the class here which MATLAB uses to do a lot of video writing operations here so the constructor for this is I just need to give it the name of the movie so in this case I don't know how about curve since we're drawing the curve of this trajectory here okay and then what I'm gonna do here is let's go ahead and set this thing to a variable name maybe call it my writer writer gets there we go okay so at the end of line 50 we've basically created this video writer object and now what we can do here is we can go ahead and modify parameters of this like so for example maybe the frame rate I want this to the final movie to play at 20 frames per second so I'll do that okay so now that I've got my my video writer object set up and ready to go what I can do here is I can open the video writer object right the movie and close the file that's my next step here so I'm just gonna call it and say open my writer and then I can call the right video function here and just pass it the my writer right which was that video writer object here and then I have to pass it the the flip book here right so if you remember right the video writer is just the object that's gonna do the writing it doesn't actually have the data the movie vector is the actual data or my foot book so I'm gonna pass a movie vector so at this point right video should know everything it needs to to create that so that 5955 is gonna actually generate the AVI file on my hard drive and when I'm done let's go ahead and close my writer alright so that should be the bare-bones amount of code to get this working here so before we run this here maybe let's switch here to look at the current folder and notice here yeah there's nothing called dot avi right now but as soon as we run step 5 we should hopefully see something show up there so here's our picture here we're seeing the animation go through and we're generating the flip book right now and then as soon as it finishes we will write it out to a file and there we are there's a file called curve avi so if we come here to Windows Explorer here in that location here it is curve got a VI and I can just now go I double click this and run it and here's my movie saved in MATLAB here or sorry save does an AVI file on my hard drive all right now there's a couple of other options that we might want to look at here so for example there are there a different file format so I know not many people are comfortable with avi is a lot of people like mp4 is here so you can easily change your call to the constructor here a video writer and you could say I want this to be an mp4 so if you just patch it an additional parameter in the constructor the video writer will now know that hey I need to be generating an mp4 instead of an AVI file so if we run this thing again it will actually work quite nicely here so let's give this a second to finish here and when it does let's come over to our temp folder here and you see now there's a current mp4 which also should have a very similar set of information so again here's my animation running at 20 frames per second here great one other last thing that I wanted to show here was actually you know you can start doing fancy things like for example maybe well let's make the the viewpoint change here so instead of just staying at a static viewpoint we could go ahead and make this change so maybe let's rotate the azmuth with I don't know something like something like this here right so what this is gonna do here is the azmuth should start rotating around and around and around as we move now if we do this here you see that yeah this looks this is looking pretty cool so we still see the particle moving up but at the same time the trajectory is rotating around or the viewpoint is rotating around but now when we actually come to the video right portion we see we run into this error here what's ending up happening here is again MATLAB when you try to call get frame it tries to be intelligent about where to grab the frame here and the problem here is that the movie vector if we look at the first element of movie vector you see that it's 515 pixels by 652 pixels but a couple of seconds later when the viewpoint has switched here like here's that 10 10 frames later you see that the way actually that's that's still the same here let's let's try a little further along no not that one I should be able to find an example here where it's not the same here cuz that's basically what is yelling at us for Wow why am I having such a difficult time fun these all look the same to me here okay there we go there we go okay so here um sorry maybe a little let me let me clear this so when we look at 97 here's let me clear the screen so movie vector so one and then movie vector so 97 here so if you look here that the the height of these two images are not the same you're right one has an extra pixel here so basically what happen is get frame decided to grab a section of the screen which was a little bit larger than what we won't wanted so what do I would like to do to fix this problem here you can actually tell get frame to only grab a certain number pixels here so in order to do that what I'm gonna first do is come back up here to the figure call here and I'm actually gonna sign a figure handle to this bigger so now Figg H is gonna refer to this single figure so now when I come down here to get frame what I can do is I could say all right get frame grab big H here and now I'm gonna pass it a rectangle which says first I'm gonna specify how many pixels away from the lower left corner do I want to start grabbing so let's grab 10 and 10 so this is a little bit in so 10 pixels in from the bottom lower left corner of the picture and then let's grab how about 520 pixels wide by 400 right I'm just gonna pick some number here so there if I do this and run this script now we see that yes we again get the situation where the entire viewpoint is spinning and everything is animating but get frame is only grabbing a very specific box that I've specified down there and now that's going to ensure that the flipbook everything has the same size so when I come down here to my video writer here it will be consistent and all the pictures can be pieced together nicely so again if I come here to my curve dot mp4 we should see here's my nice rendered animated situation where the whole thing is spinning nicely here great so um with that I think that's mostly what we wanted to cover let's take a look here at a summary slide to do recap all the things we discussed today all right so to summarize what we talked about today we first started looking at just animation concepts and looked at how a flip book is actually a really good analogy for the workflow that we're gonna follow in MATLAB so that workflow we talked about next was really this this basic five step process where first we generated some data next we would draw or render the scenario at a given time we would then take a snapshot of that scenario here and then just advance time over and over and over until we were done with all of the frames of animation and once we were done the last step five was to go ahead and just save that movie here to your hard drive and then the last thing we looked at today was actually creating an example script in MATLAB using all of the different functions like get frame and video writer here to create an animation and actually create an avi or an mp4 file on your machine so I hope you enjoyed the video and it was helpful if so please subscribe to the channel you should be able to do so using the link that should have just appeared on your screen as we're gonna have lots of other videos discussing other topics in MATLAB and areas of interest so I hope to catch you at a future video thanks so much bye
Info
Channel: Christopher Lum
Views: 67,207
Rating: 4.9604206 out of 5
Keywords: Matlab, Matlab Tutorial, Matlab Movie, Matlab Animation, getframe, Matlab getframe, VideoWriter, Matlab VideoWriter
Id: 3I1_5M7Okqo
Channel Id: undefined
Length: 27min 0sec (1620 seconds)
Published: Mon Nov 05 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.