%% A few more ways to make arrays
% Generates a 1x10 array of integers from 1 to 10
X=(1:10)
X=(1:.1:10) %We can increment by other values, like the 'for' loop
X=(1:10).' %use the non-conguate transpose to make a 10x1 matrix
%Note: .' is non-conjugate transpose
% ' is conjugate transpose
%Try them both on:
ComplexArray = [1, i]
ComplexArray.'
ComplexArray'
%They, of course, have identical results on real valued arrays
%make an 1000x1 array of equally spaced numbers that starts at 1 and ends
%at 10
X=linspace(1,10,1000).'
% See also: logspace
%% Plotting
%Generate some synthetic data for plotting
X=linspace(1,10,1000)
Y=sin(X*10)
F=figure() % This creates the figure
Ax=axes() % This creates a set of axes within the figure
P=plot(X,Y) %This plots Y vs X in the axes
% 'F', 'Ax', and 'P' are objects that have properties we can modify.
%Here are some we often interested in changing
Ax.XLim
Ax.XLim=[-10 20] % Sets new limits on X-axis
Ax.XLabel.String='Time (s)' %Changes the axis label
Ax.YLabel.String='Signal (V)' %Changes the axis label
P.LineWidth=5 % makes the plot line thicker
%Many of these common changes have a shorthand version:
% short hand:
%This generates the same plot as above, but we don't have figure, axes, and
%plot line objects saved in a variable
figure
plot(X,Y,'Linewidth',5)
axis([-10 20 -1 1])
xlabel('Time (s)')
ylabel('Signal (V)')
%We can use other linestyles like this (see 'help plot')
figure
plot(X,Y,'.','Linewidth',5)
xlabel('Time (s)')
ylabel('Signal (V)')
%or like this:
F2=figure() % This creates the figure
Ax2=axes() % This creates a set of axes within the figure
P2=plot(X,Y) %This plots Y vs X in the axes
% 'F', 'Ax', and 'P' are objects that have properties we can modify.
%Here are some we often interested in changing
Ax2.XLabel.String='Time (s)' %Changes the axis label
Ax2.YLabel.String='Signal (V)' %Changes the axis label
P2.LineWidth=5 % makes the plot line thicker
P2.LineStyle='none'
P2.Marker='.'
close(F) %Close a specific figure
close all %Close all figures
%% Making and Plotting 2D Data
%Make an X and Y array that will define the spacing in a 2D grid
X=linspace(-1,1,8)
Y=linspace(-2,2,10)
%Make an array of X values and an array of Y values. Look carefully at the
%output.
[XGrid,YGrid]=meshgrid(X,Y)
%Make an anonymous function ('doc anonymous functions')
%This is a quick way to make a simple function without making a 'gauss2D.m'
%function file.
%This function takes two input arguments
gauss2D=@(A,B)exp(-A.^2).*exp(-B.^2) % Note the element-wise operation
% specified by '.' before the operations
%Call the function to create our matrix of surface height values
Z=gauss2D(XGrid,YGrid)
%Plot the surface (using shorthand method)
figure
S=surf(X,Y,Z)
xlabel('X (m)')
ylabel('Y (m)')
zlabel('Height (m)')
title('Surface plot') %figure title
legend('Data 1') %Data legend