Download presentation

Presentation is loading. Please wait.

Published byMeghan Bruce Modified over 4 years ago

1
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1

2
ELG 3120 Signal and System Analysis 2 I. MATLAB DESKTOP

3
ELG 3120 Signal and System Analysis 3

4
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);

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.

6
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

7
ELG 3120 Signal and System Analysis 7 Workspace Browser (2)

8
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.

9
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.

10
ELG 3120 Signal and System Analysis 10 Current Directory Browser (2)

11
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

12
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.

13
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.

14
ELG 3120 Signal and System Analysis 14

15
ELG 3120 Signal and System Analysis 15 II. MATLAB BASICS

16
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 = [9] 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.

17
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

18
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.

19
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].

20
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.

21
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.

22
ELG 3120 Signal and System Analysis 22

23
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)

24
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.

25
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)’)

26
ELG 3120 Signal and System Analysis 26

27
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.

28
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

29
ELG 3120 Signal and System Analysis 29 Good Luck!!!!

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google