Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 2 Introduction to MATLAB

Similar presentations


Presentation on theme: "Lecture 2 Introduction to MATLAB"— Presentation transcript:

1 Lecture 2 Introduction to MATLAB
Dr .Qi Ying

2 The MATLAB Environment
The “Desktop” Environment Command Window (Interactive commands) Edit/Debug Window Workspace Browser Figure Windows Help Window

3 Variables and Arrays The fundamental unit of data in MATLAB is the array Vectors (1 dimension, row vector or column vector) Matrices (2 or more dimensions) Scalars are treated as arrays too (1x1 array) A MATLAB variable is a region of memory containing an array indexed by the variable name Naming convention letters and numbers, ‘_’ Case sensitive: use only lowercase names to avoid confusion Only first 63 characters are significant Pick meaningful names (year, month, exchange_rate, etc. are good names; a1, ccc, xx are examples of bad names) Predefined variables (pi, i, j, clock, date, eps, ans) Unlike many other programming languages (Fortran, C, C++…), MATLAB variables are not type specific: No need to specify the variable type (integer, real, double precision, or character) before using the variables

4 Initializing variables
Method 1: Assignment statement var = expression Note: when defining an array, make sure the number of elements in every row and column are the same, or an error will occur Use semicolon(;) to suppress the auto-echoing of the variable value in the command window The colon (:) operator can be used to assign a series of equally spaced values Built-in function (zeros, ones, eye) Multiple assignments can be done in a single line, using ‘;’ to separate the assignment statement Method 2: Interactive input from keyboard age=input(‘Please enter your age:’) Method 3: Read data from a data file (load, dlmread, etc.) array1=load(‘mydata.dat’)

5 Multidimensional Arrays
Row 1 (1,1) Row 2 Row 3 (3,1) (3,4) Col 1 Col 4 Sequence in the memory: (1,1) , (2,1), (3,1), (1,2), (2,2), (3,2), … (2,4), (3,4) Elements in a multidimensional array can be addressed using a single index. For example: a=[1 2 3; 4 5 6] The value of a(3) will be 2.

6 Subarray Elements of the subarray can be selected by: Colon operator
a(1:3, 2:5), a(:,2), a(4:end,2:end) A vector of array index a([1 4 7], 2), a([1 2 3], 4:end)

7 Display output data disp command fprintf: formattable display function
>>x= >>fprintf(‘My number is %6.2f\n’, x); My number is %: indicator of a formatted variable to be displayed 6: saves at least 6 characters 2: number of decimals f: real number (with decimals). Other data types are: d: integer; e: scientific notation g: let matlab choose f or e itself Note: the data type must match the variable to print. \n: change line. x: the numerical variables to be printed.

8 Operations Scalar Matrix
Element-by-element operation (.). For example, a.*b vs. a*b Left division (\). For example, x=A\b, which is equivalent to x=inv(A)*b. Useful for solving simultaneous linear equations

9 Basic 2D Plotting Basic plotting function in MATLAB is ‘plot’ Example:
Plot colors, marker styles and line styles: x=0:0.1*pi:2*pi; y=sin(x); plot(x,y,’o-’) legend(‘y=sin(x)’)

10 Basic 2D Plotting Subplot - Generate a figure with multiple-panels
x=0:0.1*pi:2*pi; y1=sin(x); y2=cos(x); subplot(1,2,1); plot(x,y1,'o-'); xlabel('x'); ylabel('y=sin(x)'); title('(a)‘); legend('y=sin(x)') subplot(1,2,2); plot(x,y2,'x:'); xlabel('x'); ylabel('y=cos(x)'); title('(b)') legend('y=cos(x)','Location','Best')

11 Save a plot Using the menu in the figure window Using command print
Can be used in the MATLAB program to automatically save the figure. >> print -dpng test2_ver2.png Other useful formats: -djpeg JPEG image -deps encapsulated postscript image -dtiff compressed TIFF image

12 M-file A series of commands can be save in the M-file as a program. For example, the plotting codes used in the previous example can be entered using the editor:

13 M-file MATLAB will search under current directory for M-files.
By default, all M-files will have suffix .m in the file name. For example, test1.m. You don’t need to type the .m suffix when executing a program

14 Directory related commands
Current directory: pwd Make a directory: mkdir Change to a directory: cd >> pwd ans = C:\Users\qying\Documents\MATLAB >> mkdir test2 >> cd test2 Special command: cd .. Go to the parent directory

15 Directory related commands
List files in the current directory >> cd lab1 >> dir test1.png test2_ver2.png test2.m untitled.png test1.m test2.png Special files: .. parent directory Current directory


Download ppt "Lecture 2 Introduction to MATLAB"

Similar presentations


Ads by Google