4 and 5 mat
TRANSCRIPT
-
8/11/2019 4 and 5 MAT
1/15
`
Ex:No:4 Input-Output Functions, Reading and storing Data
Data Input:
Three common ways to input data in MATLAB
- Assign data to different variables through an assignment
statement
- Input data from keyboard as per commands displayed on the
monitor
- ead data from a file! which has been already stored in the
computer"s memory
Assignment statement and variable declaration
#ariable name $ constant value
Inde%$&'
Examples
To create a scalar variable p and to store the number &( in it
)$&('
Interactive Inputs:
*+ Input function
,+ eyboard command
.+ Menu function
&+ )ause command
Input Function:
r$input/01nter radius in meters"+'
The input can be of any e%pression2 If the input data should be returned to
the user as a character string! only then the input function includes
character 0s"2
r$input/0what is your father"s name3"!"s"+
Keyoard !ommand:The keyboard command when included in a script file returns control to
the keyboard at the point where the command occurs2
Example:
4rite a program to illustrate the use of keyboard command2
a$eye/5+'
for 6$*35
disp/6+'
if 6$$.
keyboard' 77 control is returned to the keyboardend'
*
-
8/11/2019 4 and 5 MAT
2/15
`
end'
menu Function:
The menu function is used to give the user an option to pictorially select a
particular choice from the given choices2
I$ menu/0Title"!"option*"!"option,"!82+
Example:Illustrate the use of menu function! to choose a particular si9e from three
different given si9es2
I$menu/0:hoose a si9e"! 0Large"!"Medium"!"small"+
If small is selected! the value of I is returned as .2
"ause !ommand:
The pause command temporarily halts the current computation and waits
for the user to give command to resume the computations2
Example:
4rite a program to illustrate the use of pause command2p$;* ,' , &
-
8/11/2019 4 and 5 MAT
3/15
`
- dlmread function
- dlmwrite unction
- te%tscan and te%tread functions
load Function:The load function is used to load variables7data in workspace from the disk file2
$yntax:
load
load/0filename"+
load/0filename"!"%"!"y"!"9"+
load filename %! y! 9
The load/0filename"+loads all the variables from the filename given2
test$load/0abc2t%t"+
test$* . & 5 ?
> ? @ ( C
save Function:
The save function saves the current workspace data7variables in the disk file2 It
saves data7variables from workspace to disk whereas load function saves
variables from disk to workspace2
$yntax:
save
save/0filename"+save/0filename"!"a"!"b"!"c"+
save filename a! b! c
Example:
save/0check"! 0a"! 0b"+
The variables a and b are stored in filename check2mat in the current directory2
dlmread Function:
The dlmread function is used for reading numeric te%t data separated by
character other than space2
$yntax:
data $ dlmread/0filename"+
data $ dlmread/0filename"! delimiter+
Example:
varDname$dlmread/0abc2t%t"! "'"+dlm%rite Function:
The dlmwrite function is used to write a matri% to an AE:II-delimited file2
$yntax:
dlmwrite/0filename"! data+
.
-
8/11/2019 4 and 5 MAT
4/15
`
It writes matri% named 0data" into AE:II format file using the default
delimiter/!+2
dlmwrite/0filename"! data! 0F"+
It writes matri% named 0data" into AE:II format file using the delimiterF to
separate matri% elements2
textscan and textread Functions:The file needs to be opened for read operation using fopen command and then
read using te%tscan command as illustrated below3
fid $ fopen/0abc2dat"! 0r"+
varDname $ te%tscan/fid! 0Gf Gc82"+'
The above statements will read the data into varDname! a cell array2
The command te%tread can be used to read the data into separate variables2
$yntax:
;a! b! c! d
-
8/11/2019 4 and 5 MAT
5/15
`
The functions are
*2 ile opening and closing functions
,2 Binary input-output functions
.2 ormatted input-output functions
File Opening and !losing Functions:
&open Function:
fid $ fopen/filename! permissionDcode+
where fid is a file id assigned to the file and it is a positive number
filename is a character string specifying the name of the file to be opened
permissionDcode is a character string specifying the mode in which the file
opened2
&close Function:After using a file for different operations! it should always be closed2
status $ fclose/fid+
status returns the result of the operation
fid specified file id of the file to be closed
status returns ( if the operation is successful and returns -* otherwise
Formatted Input-Output Functions:
The different formatted input-output functions are given as
*2 fprintf unction
,2 fscanf unction
.2 fget* unction
&2 fgets unction
&print& Function:
syntax:
fprintf/fid! format! data+
Example:
Illustrate the use of fprintf function to varying display the values of % varying
from ( to *@( with four e=ual intervals and also display its sine values2
%$(3 &5 3*@('
y$;%' sind/%+
-
8/11/2019 4 and 5 MAT
6/15
`
C(2(( *2((((
*.52(( (2?(?*
*@(2(( (2((((
&scan& Function:
The fscanf function reads formatted data in a user-specified format from a file2
Eynta%3A $ fscanf/fid! format+
;A! :ount< $ fscanf/fid! format! si9e+
where fid is file id of a file to be read
format specifies how data is to be stored
A is variable to store the data
count returns the number of values read from the file
si9e specifies the amount of data to be read from the file
Example:Illustrate the use of fscanf to read a file test2t%t having first ten integer numbers
in
a+ /,J5+ array format
b+ Kne column
c+ /5J,+ array format
A file named test2t%t having first ten integers is created2
a+ /,J5+ array format
id $ fopen/0test2t%t"! 0r"+'
;a! count
>
-
8/11/2019 4 and 5 MAT
7/15
`
?
@
C
*(
count,$*(
c+ /5J,+ array format
fid $ fopen /0test2t%t" ! 0r"+
;a! count.< $ fscanf/fid! 0Gf"! ;5!,
, ?
. @
& C
5 *(
count.$*(
&get) Function:
The fget* function reads the ne%t line e%cluding the end of line characters from
a file as a character string2
Eynta%Etatement $ fget*/fid+
4here
fid is file id of a file to be read
statement is character array that receives the data2
&gets Function:
The fgets function reads ne%t line from file including the newline character2
$yntax
Etatement $ fgets/fid+
wherefid is the id of a file to be read
statement is character array that receives the data2
*inary Input + Output Functions:
The formatted input-output functions works with human readable te%t in a file2
This file is also called an AE:II te%t file2
*2 fwrite unction
,2 fread unction
&%rite FunctionThis function is used for writing binary data in a user-specified format to a file2
?
-
8/11/2019 4 and 5 MAT
8/15
`
$yntax
count $ fwrite/fid! array! 0precision"+
where
count is number of values written to the file
fid is file id of a file opened with fopen function
array is an array of values to be writtenprecision is used to specify the format for storing the values2Example:
Illustrate the use of fwrite function for writing binary data of different formats
to a file named check2t%t
a$;, & >' . 5 ?
-
8/11/2019 4 and 5 MAT
9/15
`
clear all
fid* $fopen/0check2t%t"2 0r"+
;A! count*;B! count,;:! count.:$:"':$setstr/:+
fclose/fid*+
C
-
8/11/2019 4 and 5 MAT
10/15
`
Experiment-.Aim:
Vectors and Matrices, commands to operate on vectors and matrices,
matrix Manipulations.
Explanation:
Ecalar #ector3
A Ecalar is a /*H*+ matri% containing a single element only2
1g3 =$*('
:olumn #ector3
A :olumn vector is a /mH*+ matri% that has m number of rows but a single
column only2
1g3 =$;* , & (
& @ (
5 *( (
#ector Transpose3Transpose operation turns a row vector into a column vector and vice versa2 The
command for transposing % is x/
Eg: if %$;. & 5< then
H"$ .
&
5
:reation of 1venly spaced ow vectors3
A row vector with evenly spaced! real elements can be created by using the following
command3
#ariable name$k3l3m!
4here k is the initial value of the row vector
l is the step si9e or increment and
m is the final value of the row vector
1g3 t$*3,3**
4ill create a row vector with initial element$*! and final element$** with a step si9e
of ,! given as3
t$ ;* . 5 ? C **!,+
This will result into a >H, reshaped matri% B! shown as
B$ * .
5 ?
C **
*.
-
8/11/2019 4 and 5 MAT
14/15
`
, &
> @
*( *,
1%panding Matri% Ei9e3
4hen a single element or a few elements are entered in a mtri%! Matlab creates the
matri% of proper dimensions to accommodate the entered elements2 emaining unspecified
elements are assumed to be 9ero21g3 :/,!,+$*(
Then
:$( (
( *(
Appending a row7column to a matri%3
A column can be appended by using following command3
A$;A H