MATLAB fplot in MATLAB®
Learn how to make 7 fplot charts in MATLAB, then publish them to the Web with Plotly.
Plot Expression
Plot sin(x) over the default x interval [-5 5].
fplot(@(x) sin(x)) fig2plotly()
Plot Parametric Curve
Plot the parametric curve x=cos(3t) and y=sin(2t).
xt = @(t) cos(3*t); yt = @(t) sin(2*t); fplot(xt,yt) fig2plotly()
Specify Plotting Interval and Plot Piecewise Functions
Plot the piecewise function
ex -3<x<0
cos(x) 0<x<3.
Plot multiple lines using hold on. Specify the plotting intervals using the second input argument of fplot. Specify the color of the plotted lines as blue using 'b'. When you plot multiple lines in the same axes, the axis limits adjust to incorporate all the data.
fplot(@(x) exp(x),[-3 0],'b') hold on fplot(@(x) cos(x),[0 3],'b') hold off grid on fig2plotly()
Specify Line Properties and Display Markers
Plot three sine waves with different phases. For the first, use a line width of 2 points. For the second, specify a dashed red line style with circle markers. For the third, specify a cyan, dash-dotted line style with asterisk markers.
fplot(@(x) sin(x+pi/5),'Linewidth',2);
hold on
fplot(@(x) sin(x-pi/5),'--or');
fplot(@(x) sin(x),'-.*c')
hold off
fig2plotly()
Modify Line Properties After Creation
Plot sin(x) and assign the function line object to a variable.
fp = fplot(@(x) sin(x))
fp =
FunctionLine with properties:
Function: @(x)sin(x)
Color: [0 0.4470 0.7410]
LineStyle: '-'
LineWidth: 0.5000
Show all properties
fp.LineStyle = ':'; fp.Color = 'r'; fp.Marker = 'x'; fp.MarkerEdgeColor = 'b'; fig2plotly()
Plot Multiple Lines in Same Axes
Plot two lines using hold on.
fplot(@(x) sin(x)) hold on fplot(@(x) cos(x)) hold off fig2plotly()
Add Title and Axis Labels and Format Ticks
Plot sin(x) from -2π to 2π using a function handle. Display the grid lines. Then, add a title and label the x-axis and y-axis.
fplot(@sin,[-2*pi 2*pi])
grid on
title('sin(x) from -2\pi to 2\pi')
xlabel('x');
ylabel('y');
fig2plotly()
Use gca to access the current axes object. Display tick marks along the x-axis at intervals of π/2. Format the x-axis tick values by setting the XTick and XTickLabel properties of the axes object. Similar properties exist for the y-axis.
ax = gca;
ax.XTick = -2*pi:pi/2:2*pi;
ax.XTickLabel = {'-2\pi','-3\pi/2','-\pi','-\pi/2','0','\pi/2','\pi','3\pi/2','2\pi'};
fig2plotly()

