MATLAB is great at working with higher dimensional data. Learn how to generate a 3D matrix and plot the results.
- [Instructor] So MATLAB is great at plotting two dimensional data and dealing with images, and we can also deal with three dimensional data while we are at it. Let's go ahead and close these images that we have open, do close all. Also clear the workspace and the screen. Let's close image time, let's create ourselves a new script and we'll call this one ThreeD. Let's open that one up, and let's put a comment at the top here, let's say Create some...
3D data and plot it. So most of the time when I'm dealing with three dimensional data, I use the function mesh grid to generate the x and y coordinates that I need for my plot. So it's going to look like this. We're going to create an array x and an array y and we're going to get that from the function mesh grid. As with most MATLAB functions there's a lot of things we can do with mesh grid, but we're just going to create a square grid for now, cause that's really simple.
So we'll pass it a vector, to generate that square grid. So let's go up here and define that vector. For this example we'll do some sines and cosines, that'll be fun, so let's create our grid based around pi, where say our vector here is going to go from -2*pi in steps of... .5 to 2*pi. I'm picking the step value somewhat arbitrarily here, it'll just make for a nice plot in the end of course.
If we were doing real calculations and plotting real data we'd want to be more deliberate about what we'd pick. So by passing our vector here from -2*pi to 2*pi into mesh grid, this will generate capital X and capital Y as two dimensional arrays. Once we have those generated, we can calculate the height value for our three dimensional plot we'll call that capital Z, and let's just do something fun here again, kind of arbitrary but it'll look neat when we're done with it. We'll do sin(X) + cos(Y) and MATLAB does also have some built-in functions that we could have used if we wanted to, I would encourage you to check out the function peaks, it's kind of fun.
Now that we have our X, Y and Z data generated, let's plot them. We'll do figure... we'll spin up a new one here, figure(4) and then we're going to use the command surf, for surface. And this is how we plot three dimensional data. And all we do is pass it X,Y and Z, make sure you use the capitals there, let's run the script, and there we go, we have a cool three dimensional plot with some sines and cosines in it. I'm going to go ahead and maximize this and once nice thing we can do to visualize our data is if we click here on the box with the arrow around it that denotes Rotate 3D, that will allow us then to click and drag on the plot and we can take different looks at it.
Now a three dimensional plot like this acts very similar to our two dimensional plots. We can annotate them, we can add a title. We'll call this 'Super cool Three Dee' We can add labels, we can do an x label or a y label or in this case we can do a z label, and we'll call our z label here 'Ultimate Power' that's clearly what we're measuring, if we go ahead and run this now we can see our new labels have appeared.
One last thing I want to point out, is a neat function called color bar. This'll add a color bar to the side of our plot showing the maximum and minimumn values. Again, I would encourage you to explore the help documentation on surf, and color bar and some of these other functions, as there is a lot more functionality that we don't have time to cover here.
- Creating MATLAB variables
- Working with matrix and scalar operations
- Using if statements and loops
- Creating functions
- Importing data
- Building basic plots and 3D plots
- Working with images
- Creating Simulink models