1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr....

32
1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept. http://www.bmabdelaty.faculty.zu.edu.eg/ Default.aspx

description

Agenda 3  Using Script Files and Managing Data  Input Command  Output Commands: disp, fprintf  Save / load Commands  Importing / Exporting Data.

Transcript of 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr....

Page 1: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

1

Lecture 4

Post-Graduate Students

Advanced Programming (Introduction to

MATLAB)Code: ENG 505

Dr. Basheer M. Nasef

Computers & Systems

Dept.http://www.bmabdelaty.faculty.zu.edu.eg/Default.aspx

Page 2: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

2

In the previous lectures

Matlab Windows (workspace, m-file,…)

Working with Arrays

Mathematical Operations with Arrays

Generating Random numbers

Page 3: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

3

Agenda

Using Script Files and Managing Data

Input Command

Output Commands: disp, fprintf

Save / load Commands

Importing / Exporting Data.

Page 4: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

4

To enable the user to assign a value to the variable

in the run-time. This is done by using the input

command for creating the variable.

Input Command

Page 5: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

5

Example: Chapter4 Example4.m

Input Command

Page 6: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

6

Two commands that are frequently used to generate output are disp and fprintf. The disp command displays the output on the screen.The fprintf command can be used to display the output on the screen or to save the output to a file.

Output Commands

Page 7: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

7

The disp command:

disp

Example: PopTable.m Displaying data in a table

Page 8: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

8

Example: PopTable.m

disp

Page 9: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

9

The fprintf command: Using the fprintf command to display text:

Using the fprintf command to display text and numerical values:

fprintf

Page 10: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

10

fprintf

– (minus sign): Left-justifies the number within the field.

precision specifies the number of digits to the right of the

decimal point.

Page 11: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

11

The last element in the formatting elements, which is required, is the conversion character, which specifies the notation in which the number is displayed. Some of the common notations are:

fprintf

Page 12: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

12

Example: Chapter4Example7.m

fprintf

\n: moves to a new line

Page 13: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

13

Using the fprintf command to save output to a

file:

When dealing with files, be careful with the path of

the file. (it is better to have the file in the current

directory)

fprintf: saving output to a file

Page 14: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

14

Step a:

fprintf: saving output to a file

file identifier.

Page 15: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

15

Step b:

Step c:

fprintf: saving output to a file

Page 16: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

16

Example 5:

fprintf: saving output to a file

Page 17: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

17

Example 5:

fprintf: saving output to a file

Page 18: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

18

fprintf: saving output to a file

Example 5:

Page 19: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

19

The save command: you can save all variables in the work space:

Save to file

All of the variables currently in the workspace are saved in a file

named file_name.mat that is created in the current directory. In

mat files, which are written in a binary format, each variable

preserves its name, type, size, and value. These files cannot be read

by other applications.

Page 20: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

20

The save command: you can save some variables in the work space:

Saving variables in ascii format:

Save to file

Page 21: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

21

The load command: is used to retrieve data from (.mat) files (created by save command), ascii files or text (.txt) files.

loading data from (.mat) files:

Load from file

Page 22: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

22

loading data from text files (.txt):

Load from file

Page 23: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

23

Import from Excel sheet:

Importing and exporting data

Export to Excel sheet:

Page 24: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

24

Importing and exporting data

Example: importing data from Excel sheet:

Page 25: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

25

Exercise

Page 26: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

26

Exercise: solution: m-file

Page 27: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

27

Exercise: solution: result in Command Window

Page 28: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

28

Exercise Modification

Solve the previous exercise but instead of using the input command assume that the values are stored in Vinput.txt file, also save the results in Vresults.txt file.

Page 29: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

29

solution: input text file and m-file

Page 30: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

30

solution: output text file

Page 31: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

31

Material & problems

We have covered Chapter 4 from Gilat.

Problems you can try:

Sample Problem 4-1

1,2,5,10,19,22

Page 32: 1 Lecture 4 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.

32

Assignment #1, Due Date is 19/11/2015

Problem: 23 in section 4.7, page 130 in Gilat

You have to deliver a screen shoot of your Matlab code and result.