If the input vector contains complex numbers, MATLAB plots the real part of each element Vector will be used for the horizontal axis). In that case the vector columns are plotted versus their indices (the The plot command can also be used with just one input vector. MATLAB can plot a 1 x n vector versus an n x 1 vector, or a 1 x n vector versus a 2 x n matrix (you will generate two lines), as long as n is the same for both vectors. One thing to keep in mind when using the plot command is that the vectors x and y must be the same length.
Plot function matlab code#
If we wished to plot this function, we could create an m-file with the following code to generate the basic plot shown below. Consider the following simple, linear function.
Plot function matlab how to#
The default is that each time the plot command is issued, the current figure willīe erased we will discuss how to override this below. This command will plot the elements of vector y (on the vertical axis of a figure) versus the elements of the vector x (on the horizontal axis of the figure). This code can be entered in the MATLAB command window or run from an m-file. The basic syntax of the function call is shownīelow. The plot command also happens to be one of the easiest functions to learn how to use.
Plot function matlab update#
The call to drawnow isn't neccessary in each iteration, it is only used to update the visuals, so you can see the changes directly.One of the most important functions in MATLAB is the plot function. H_stairs = stairs(h_axes, x, y, 'XDataSource', 'x', 'YDataSource', 'y') % and bind the variables x and y as its data sources % Create graph object, in this case stairs Therefore I suggest using datasources: Update graph using data sources % Create a panel and axes object If you want to draw a curve as you add data, try the following: x = linspace(0,2 * pi, 100) īe aware that the plot automatically tries to set x and y limits (curve is scaled to the plot window), to prevent that you have to manually set the x- and y-limits with xlimand ylim.Īs Matt wrote in his answer, calling plot in each iteration is quite expensive (i.e. Note that calling plot every time within the for-loop is unnecessarily expensive and I don't recommend it. As a last step you can update the plot by changing its XData and YData properties and calling drawnow. Within the for-loop calculate the values and add them to the y-vector as shown above. In case you insist on plotting within each iteration, the previous code from Solution 2 can be expanded as follows: Create a figure, add an 'empty' plot to it and store its handle. Solution 3: Dynamically update plot while calculating If you want to calculate the values within a for-loop and plot it afterwards: Pre-allocate the needed variable (in this case y), calculate the values within the for-loop and finally plot it with one single command after the calculation. Solution 2: Calculate values within for-loop and plot afterwards If you want to plot the points itself, use LineSpec-syntax when calling plot like this 1: plot(x,y,'*') ġ) Other types of points are possible as well, see the above linked documentation. Note that y is a vector as well as x and that y(n) equals to sin(x(n)) for all n. So the following code does probably what you want: x = linspace(0,2*pi,100) In that case no for-loop is needed because you can calculate and plot vectors directly in MATLAB. I assume you meant to draw a continuous line. Solution 1: Vectorized calculation and direct plot With plot(x(i),y) you are plotting 100 single points (one in each iteration) and they are not shown by default.