4. Plotting Data - Arduinerd/PDS-MatLab GitHub Wiki

Native MatLab Plots

In order to plot with just built-in MatLab functionality, it is necessary to declare arrays with the domain and range.

Declare an array with n elements between start and end inclusively:
x=linspace(start,end,n)
It is possible to plot just the range but the view will not fit exactly

Plotting in 3D requires a 2x2 array to be declared:
[x y] = meshgrid(xDomain, yDomain) After defining the range of z, you can plot by the surf(z) command

Plots from Toolboxes

Within the Symbolic Toolbox there are a set of functions which make plotting much easier than via the native MatLab way.

Plotting y=x:
>>ezplot('y=x')

The default range of ezplot is set to -2pi to 2pi

or
>>syms x
>>fplot(x)

The default range of fplot is set to -5 to 5

Plotting x^2+y^2 = 9:
>>ezplot('x^2+y^2 = 9')
or
>>syms x y
>>fimplicit(x^2+y^2==9)

Plotting Parametric Functions:

>>syms t
>>x = t*sin(5*t);
>>y = t*cos(5*t);
>>ezplot(x, y) or fplot(x,y)

  • By adding a second argument to any of these functions, you can specify the domain in an array
  • You can additionally declare a symbolic function through syms f(x, y) or syms f(x)
  • Setting a symbolic function to a given equation (without quotes) will allow you to use the function afterwards

Customizing Plots

Adding features requires you to set a plot equal to a variable or to edit the figure as a whole.

Start by creating a figure with >>figure and entering in >>hold on. This will cause the graphs to not reset upon adding a new function.

When adding a function to fplot() each will have a different color. However, each ezplot() will be the same unless you change the color manually.

Set X Label
>>xlabel("labelX")
Set Y Label
>>ylabel("labelY")
Set Line Color
>>plot(x,y,'color','red') or >>ez = ezplot('y=x')
>>set(ez,'color','red')
Create Legend
>>legend("line1","line2", ...)
Set Title
>>title("title")

If you want to create these from a script, you can see the code by selecting "Generate Code" in the File menu of the figure window

Example Using Ezplot()

Example