1 lecture 4 post-graduate students advanced programming (introduction to matlab) code: eng 505 dr....
DESCRIPTION
Agenda 3 Using Script Files and Managing Data Input Command Output Commands: disp, fprintf Save / load Commands Importing / Exporting Data.TRANSCRIPT
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
2
In the previous lectures
Matlab Windows (workspace, m-file,…)
Working with Arrays
Mathematical Operations with Arrays
Generating Random numbers
3
Agenda
Using Script Files and Managing Data
Input Command
Output Commands: disp, fprintf
Save / load Commands
Importing / Exporting Data.
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
5
Example: Chapter4 Example4.m
Input Command
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
7
The disp command:
disp
Example: PopTable.m Displaying data in a table
8
Example: PopTable.m
disp
9
The fprintf command: Using the fprintf command to display text:
Using the fprintf command to display text and numerical values:
fprintf
10
fprintf
– (minus sign): Left-justifies the number within the field.
precision specifies the number of digits to the right of the
decimal point.
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
12
Example: Chapter4Example7.m
fprintf
\n: moves to a new line
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
14
Step a:
fprintf: saving output to a file
file identifier.
15
Step b:
Step c:
fprintf: saving output to a file
16
Example 5:
fprintf: saving output to a file
17
Example 5:
fprintf: saving output to a file
18
fprintf: saving output to a file
Example 5:
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.
20
The save command: you can save some variables in the work space:
Saving variables in ascii format:
Save to file
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
22
loading data from text files (.txt):
Load from file
23
Import from Excel sheet:
Importing and exporting data
Export to Excel sheet:
24
Importing and exporting data
Example: importing data from Excel sheet:
25
Exercise
26
Exercise: solution: m-file
27
Exercise: solution: result in Command Window
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.
29
solution: input text file and m-file
30
solution: output text file
31
Material & problems
We have covered Chapter 4 from Gilat.
Problems you can try:
Sample Problem 4-1
1,2,5,10,19,22
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.