barbara/pratik/yu. outline matlab desktop m-files variables arrays plots
TRANSCRIPT
Matlab IntroductionBarbara/Pratik/Yu
OutlineMatlab desktopM-filesVariablesArraysPlots
Matlab Desktop
Command Window
Workspace
Command History
M-filesSave set of commands in a file Long code that requires more than a couple
of commandsAllows you to save codeExtension that is used in matlab
Examples -- Filename.m, anyname.m
How to create an m-fileClick on File in upper left hand sideClick on newClick on blank m-fileType codeSave as
How to open/edit/close an m-fileClick on fileClick on openThen choose the m-fileMake any changes needed to the fileClick on saveThen close the file by the x located in the
upper right hand or File -> Close
Variable Assignmentx=4 --Simple numerical
assignment
t = 10 – 4 -- Result of an operation
y = pi --Special assignment
z = 3 + y --Result of an operation with variables
ArraysVariables in Matlab can be accessed by
indexingEvery element of an array is designated by its
unique row and column or a number pair
The syntax is A(row#,column#)
VectorsVectors are 1-dimensional arraysThere are several ways to create a vectort= xmin:dx:xmaxtime= [ 1,2,3,4,5 ]t=linspace(xmin,xmax,dx)x=linspace(0,5,1)
Commandsclear----Clears the workspaceclc—clears the command windowsave – saves the file from the
command windowload—load the specified file from the
command window
Plotsplot(x,y) --plots 2-d data with x axis with
values of x and y axis with values of y
Change plot line colorFor example
plot(year,country,’b’) will make the plot line color blue
Color options g green r red c cyan m magenta k black
Change Line StyleFor example
plot(year, country,’-’) will make the line style solid
Other line styles:-- dashed: dotted-. dash dot
Change plot markerplot(year, country, ’d’) diagonal cross
Other marker options + vertical cross * star
d diamond
0.5 1 1.5 2 2.5 30
0.5
1
1.5
2
2.5
3
3.5
Y-Values
Y-Values
Plot typesBar graph
bar(x,y)
Stem graph stem(x,y)
Categ
ory
1
Categ
ory
2
Categ
ory
3
Categ
ory
40246
Bar
Bar
Annotating PlotsTo make a label for the x-axis
xlabel(‘Year’)
To make a label for the y-axisylabel(‘Country’)
To make a title for the Plottitle(‘Gas Prices’)
Creating a SignalCreate a continuous signal
F(t)=A * sin(ωt)
Define a discrete time axisThere are several ways to create the time axis—(creating a vector)
t= xmin:dx:xmaxtime= [ 1,2,3,4 ]t=linspace(xmin,xmax,dx)x=linspace(0,100,5)
Plotting dataType in the following code (excluding the comments
denoted by %):
A=2; %assign value for variable A frequency=0.5; %assign value for variable frequencyw=2*pi*frequency; %set value for w tmin=0; %assign starting time tmax=10; %assign end timedt=0.01; % assign increment time t=tmin:dt:tmax; %define time vector f=A*sin(w*t); %define the continuous time signal function plot(t,f,'r'); %plot with color of red. 'r'-redxlabel('time: t'); %set lable for x axisylabel('signal: f(t)'); %set lable for y axis title('function of continuous time signal'); %set title