PLOTTING with MATLAB
simple 2D plot
To see the plot object characteristics you have to assign the plt to a variable:
pt=plot(x,y) % assign to a variable to see line properties
pt =
Line with properties:
Color: [0 4.4700e-01 7.4100e-01]
LineStyle: '-'
LineWidth: 5.0000e-01
Marker: 'none'
MarkerSize: 6
MarkerFaceColor: 'none'
XData: [1×51 double]
YData: [1×51 double]
ZData: [1×0 double]
Show all properties
plot(x,y,'LineWidth',1.5); % change line properties
plot(x,y,'LineStyle','--','LineWidth',1.5); % change line properties
add more features: grid, title, etc.
axis([0,10,-1.1,1.1]) %give more room for the plot
title('A nice plot') %add title
two plots in the same window
legend('sin(x)','cos(x)') % to identify plots
line style
plot(x,z,'-sg') % lines and green squares (you can mixed)
In these tables you can find different line and marker options:
NaN terms are skipped when using plot
Plot a general function:
Let's plot the following function 
You have to define
like an inline function and then evaluate in a set of points
f=@(x) 10*(1-exp(-x/3).*sin(10*x));
h=0.1; % use only few points
h=0.02; % use more points
Subplots: many plots in the same window
subplot(2,2,2) %empty plot (optional, you can comment it)
subplot(2,2,3) %empty plot (optional, you can comment it)
Get points with the mouse
Selecting 4 points with the mouse, you can obtain something like the image below.
% does not work, inside a livescript, for Matlab 2019 and previous versions
[x, y]=ginput(4); %click 4 times on the window
Other 2D plots
% Bar plot of a bell shaped curve
% Stairstep plot of a sine wave
y=erf(x); %Matlab error function
polarplot(t,abs(sin(2*t).*cos(2*t)));
X = linspace(0,2*pi,50)';
Y = [cos(X), 0.5*sin(X)];
Animated Plots 2D: sinusoide
Sinusoid Animation: sin(x+P) where P is a phase value
%Assign values to the graphic window
figNumber=figure('Name','Sinusoid Animation', 'NumberTitle','off', 'Visible','on');
endP=2*pi; %max variation angle for P
incP=0.09; %increment de la Fase
x=-2*pi:0.2:2*pi; %rang of x values
axis([-2*pi 2*pi -1.2 1.2])
plot([-2*pi 2*pi],[0 0],'Color','k','LineWidth',2)
plot([0 0],[-1.2 1.2],'Color','k','LineWidth',2)
text(1.15,1.1,['Phase = ' num2str(P,'%.4f')])
Animated Plots 3D: Helix Animation
plot3(cos(fi),sin(fi),fi,'b')
plot3(cos(fi(k)), sin(fi(k)), fi(k),'marker','o',...
'markersize',8,'markerfacecolor', 'green');
Create a Video from an animation:
recordVideo=0; %=0 to see it on the screen, =1 to record the video
myVideo= VideoWriter('helix.avi'); %name for the video file
open(myVideo); %open our video file
plot3(cos(fi),sin(fi),fi,'b')
plot3(cos(fi(k)), sin(fi(k)), fi(k),'marker','o',...
'markersize',8,'markerfacecolor', 'green');
frame = getframe; %get the present plot
writeVideo(myVideo,frame); %save it to the video
if (recordVideo==1), close(myVideo); end
Mesgrid
[X,Y]=meshgrid(x,y)
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-4 -4 -4 -4 -4
-3 -3 -3 -3 -3
-2 -2 -2 -2 -2
-1 -1 -1 -1 -1
0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4