Learn Matlab Episode #8: Plotting Data

Click here to subscribe for more videos like this!

How do you plot data in MATLAB?

So today we’re going to learn about how to make plots in matlab. So one of the very simplest plots you can make is just to plot a series of data, so one very popular series is the Fibonacci sequence and that’s just adding the two previous numbers to make the current number, right. So here are the first 8 values of Fibonacci, and so to plot in sequence all I need do is call a function called plot and pass in the variable right, and so that’s Fibonacci. Now notice how the X values have been set automatically, so starts at one and increases sequentially. So the next thing we can do to make it a little more complex is to have actual values for X. So suppose I want X to increase by point one every time, so I might do 0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7. So now I have an x and y which is what you’re probably more used to when you’re creating a two-dimensional plot. To plot x and y you just pass in x and then y into the plot function, right and so now you can see the y-axis is the same as before but the x-axis is now no longer 1-8 its 0-0.7. Notice that the size of both x and y have to be equal, right so if I do this it is not going to work right. Alright, so the next thing we’re going to do is something a little bit more complex, we’re going to plot a function. So the first thing we need to learn about is a function called linspace. Linspace what that does is it creates the range of values of x for us so we just pass in where we want it to start, where we want it to end, and how many points in between. So I’m saying X is going to go from 0-100 and has 200 points in between. Now to calculate Y all I have to do is sin(x) we’re calling the sine function. In matlab functions generally apply element-wise right so every value of x you call sin(x) and then it assigns the value of that sine X into y. So I’m going to plot x and y again, and so now I see sine X going from zero to a hundred. So one thing you might notice about this is the lines are pretty jagged and it’s not a smooth graph but you know that sine X is actually a smooth graph. So the reason why that is if we look at X again let’s look at the values remember there are 200 points for about a range of 100, and so that’s a very low resolution. The last value here is 100, the second to last values about 99.5, so there’s only about 0.5 distance for every unit of X. So to give us a better granularity we might want to say you might want to reduce the range first of all and then put more points in between. So, let’s recreate X let’s say it’s going to go from 0 to 2 pi and have a hundred points in between, alright, so y is going to be exactly the same when a plot XY again. Alright, and so now you can see that it’s a lot more smooth. So adding to the complexity of this a little bit suppose I want to plot two functions on the same plot and that might be useful if you want to compare two different series, so let’s say the second function we multiplied is cosine of X say y2 = cos(x) and so we type in plot we can see some different function signatures for plot. If you look at the third one it shows us x1 y1…xn yn, so what that’s telling us is that for every pair x and y that we want to plot we just pass the min adjacent to each other. So in our case we’re going to have x and y, and since it’s the same x for both y and y2 we’re going to just pass an x again and say y2 over there. Alright, and so now you can see both sin(x) which is the blue line, and cos(x) which is the green line on the same plot and matlab automatically changes the colors for us so we can differentiate between. One thing you can do if you want to make it a little more fancy is you can specify a line style and so those are also passed in adjacent to the x and y’s, so now it’s even easier to differentiate between the two lines. So the second chart cos(x) is a bunch of dots, and I made a mistake the dashes the default that just makes a regular line. If you put in two dashes it makes them into dashes that you can differentiate between like that. Ok, so now let’s say I want to save this plot to my hard drive or maybe put it on the internet or a document, so you go to file save as. So you see the default file format here is .fig and that is a matlab file so only mad matlab can read those files. There are some other types of formats here that we can look at such as PDF. If you want to save it to a PDF and then print it out later. You can save a PNG or jpg so if you want to put in a word document or you’re writing a paper, or you want to put it on your blog post you might want to use JPEG or PNG, and so I’m going to use .fig since you probably already know what a PNG and JPG look like. Alright, so I already made a test.fig overwrote it so now that I have it, so you can see right here saved into my current working directory. If I want to open an existing figure I do that through a function called openfig, I pass in the filename and it opens back up the figure. And so the nice thing about matlab figures is that I can still zoom in, I wouldn’t be able to do that really with the jpeg would look very blurry, of course i can zoom out, I can use the hand to move around the chart. So, it opens back up the same figurehead before so I can play with it as if I had made it for the first time.

Leave a Reply

Your email address will not be published. Required fields are marked *