ELG 3120 Signal and System Analysis 4 Command Window Use the command window to enter variables and run functions and M-files. For example: A = ones(5,5);
ELG 3120 Signal and System Analysis 5 Command History Statements you enter in the Command window are logged in the Command History. In the Command History, you can view previously run statements, and copy and execute selected statements.
ELG 3120 Signal and System Analysis 6 Workspace Browser (1) The MATLAB workspace consists of the set of variables (named arrays) built up during a MATLAB session and stored in memory. You add variables to the workspace by using functions, running M-files and loading saved workspaces. To delete variables from the workspace, select the variable and select Delete from the Edit menu. Alternatively, use the “clear” function. Save the workspace to a MAT-file: File Save Workspace As Read a MAT-file: File Import Data
ELG 3120 Signal and System Analysis 7 Workspace Browser (2)
ELG 3120 Signal and System Analysis 8 Workspace Browser (3) – Array Editor Double-click a variable in the Workspace browser to see it in the Array Editor. Use the Array Editor to view and edit a visual representation of one or two-dim. arrays, strings, and cell arrays of strings that are in the workspace.
ELG 3120 Signal and System Analysis 9 Current Directory Browser (1) MATLAB file operations use the current directory and the search path as reference points. Any file you want to run must either be in the current directory or on the search path. A quick way to view or change the current directory is by using the Current Directory field in the desktop toolbar as shown below.
ELG 3120 Signal and System Analysis 10 Current Directory Browser (2)
ELG 3120 Signal and System Analysis 11 Editor Window (1) Use the Editor/Debugger to create and debug M- files, which are programs you write to run MATLAB functions. The Editor/Debugger provides a GUI for basic text editing, as well as for M-file debugging Create a new M-file: File New M-file Open an M-file: File Open
ELG 3120 Signal and System Analysis 12 Editor Window (2) “%” is for comments. Define some variables: x = 1:5; y= 3*x+1; Use MATLAB functions and programming language. Run the M-file: (if the M-file is under the current directory) In the command window, input the name of the M-file and then ENTER. In the editor window, press F5.
ELG 3120 Signal and System Analysis 13 Help Browser Use the Help browser to search and view documentation and demos for all your MathWorks products. The Help browser is a Web browser integrated into the MATLAB desktop that displays HTML documents. To open the Help browser, click the help button in the toolbar, or type “helpbrowser” in the Command Window.
ELG 3120 Signal and System Analysis 15 II. MATLAB BASICS
ELG 3120 Signal and System Analysis 16 Matrix MATLAB is designed to work with matrices, but you can also input scalars and vectors since they can be considered as matrices with dimension 1x1 (scalars) and 1xn or nx1 (vectors). Entering matrix >>z =  Assigns the scalar value 9 to the variable z >>a =[ 1 3 5] Defines a 1x3 row vector (any space(s) or tab will denote a new column). >>b = [2;4;6] Defines a 3x1 column vector (semicolon always denotes new row). >>A = [2 1 1;0 1 0;3 0 2] Defines a 3x3 matrix in the variable A.
ELG 3120 Signal and System Analysis 17 Transpose and diag A = [2 1 1; 0 1 0; 3 0 2]; A’ gives the transpose of A diag(A) gives the diagonal elements of A Subscripts The element in row i and column j of A is denoted by A(i,j). A(1,1) =2 The colon operator 1:10 is a row vector containing the integers from 1 to 10: 1 2 3 4 5 6 7 8 9 10 To obtain nonunit spacing, specify an increment. For example, 100:-7:50 is 100 93 86 79 72 65 58 51
ELG 3120 Signal and System Analysis 18 The colon operator Subscript expressions involving colons refer to portions of a matrix. A(1:k,j) is the first k elements of the j th column of A. A(:,j) is the jth column of A. A(i,:) is the ith row of the A.
ELG 3120 Signal and System Analysis 19 Operations MATLAB supports numerous matrix operations and reminds you about the dimension requirements of the operations. Define: a=[1 3 5]; b=[2;4;6]; >> c=a*b Computes the dot product yielding the scalar c with the value 44. >> d=a ’ +b Matrix addition or subtraction yields the column vector d=[3;7;11].
ELG 3120 Signal and System Analysis 20 Plotting (1) MATLAB supports a number of plot types with the ability to create custom graphs. The simplest x-y type plot can be created using plot(x,y) where x and y are vectors with a list of values stored in them. Other plot commands including: loglog, semilogx, semilogy, bar, stem, polar, plot3, contour, mesh and surf. To find out about the syntax for any of the plot commands you can type help inside of MATLAB. The figure plotted will be shown in the Figure Window.
ELG 3120 Signal and System Analysis 21 Plotting (2) Example 1: Using the colon to create lists and generating a plot >>x=[1:.5:10] Defines a vector x that goes from 1 to 10 by 0.5. The colon is used to both create a list using the syntax [lower:increment:upper] and to select specific portions of a matrix. >> y=2*x Defines a vector y with values equal to twice the values in x. >>plot(x,y) Creates a simple x-y plot of the data stored in the vectors x and y.
ELG 3120 Signal and System Analysis 23 Plotting (3) Adding text, setting scales and styles >> axis([xmin xmax ymin ymax]) Sets the x and y scales to the values you specify >> title('title text') Places a title above the plot. The commands xlabel('xtitle text') and ylabel('ytitle text') place a titles along the x and y- axes, respectively. >>text(x,y,'your text') Places any text string at the graph coordinates (x, y)
ELG 3120 Signal and System Analysis 24 Plotting (4) Styles including color and line type can be specified by using a 2 or 3 character string inside the plot command immediately following the y variable. For example, the command plot(x,y,'r--') will produce a red dashed line. The available color and line type variables are given below: For color: y yellow; m magenta; c cyan; r red; g green; b blue; w white; b black For line type:. point; o circle; x x-mark; + plus; * star; - solid; : dotted; -. dashdot; -- dashed Plotting more than one data set on an a single axis can be accomplished by using the command hold on and then plotting the additional data followed by a hold off command.
ELG 3120 Signal and System Analysis 25 Plotting (5) Example2: simple plots The following sequence of commands plots the graph of the sine function between 0 and π, provided that the two arrays have the same number of elements. >> x = 0:pi/90: pi; >> y = sin(x); >> plot(x, y) >> grid on >> xlabel(‘x, radians’) >> ylabel(‘sin(x)’)
ELG 3120 Signal and System Analysis 27 Programming Matlab Calling M-files Sequences of MATLAB commands can be written to files with the extension m, appropriately called M-files. Entering the name of the file (without the extension!) causes automatic execution of all the statements. In their simplest form, such files are called script files.
ELG 3120 Signal and System Analysis 28 Help commands Important commands for MATLAB can be counted as help, lookfor, helpdesk and helpwin. Pressing the up-arrow “↑” gives the previous command that was typed, which might be convenient while typing long codes. Finally, if you are sure about your code and MATLAB doesn’t give you the correct output, you always have the right to ask ‘why’. >> why
ELG 3120 Signal and System Analysis 29 Good Luck!!!!