Plotting a Sine Wave
x = [0:.2:6]
y = sin(x)
This will plot the sine function you just saved to the variable y. Notice that it just plotted the values of y versus the index of each data point (from 1 to 31).
This is a basic 2D plot. You can see a list of all the 2D plots available by typing help graph2d.
grid on % adds grid
Now we have the proper x-values along the bottom axis.
*If your plot window gets hidden, you can bring it to the front with 'figure(1)', which tells Matlab to make figure #1 active, or with 'figure(gcf)', which gets the handle for the current figure (gcf = 'get current figure'), and makes it active. If you give Matlab a figure number that does not exist, it will open a new figure with that handle.
A = [x;x];
surf(sin(A)) % 3D plot
You can see a list of all the 3D plots available by typing help graph3d (and also get a list of available colormaps).
figure % open a new figure
surf(sin(A')) % an apostrophe transposes the matrix A
B = repmat(x,[31 1]); % creates a 31x31 matrix by replicating x, a 1x31 matrix, 31x1 times
surf(sin(B) + sin(B')) % the interference pattern of two orthogonal sine waves
colorbar % values of color axis
xlabel('x'); ylabel('y'); zlabel('z'); % label the axes. note we can combine multiple commands on one line
Try it yourself
You can rotate the image by clicking on the circular arrow in the figure window and then clicking on the image and dragging.
view(3) % original view
view(2) % top down view
Fun with colormaps
colormap(hsv) %default is 'jet'