CHAPTER 1: INTRODUCTION INTO MATLAB COMPUTING
Lecture Notes 1.4: Reading
and Writing Operations
Interactive operations by keyboard and mouse:
·
input: takes the input data from an user, when he types a value and hits the
return key
r = input('Enter radius of a sphere: ')
r =
5
z =
input('Enter your name: ','s')
length(z)
z(1)
z(6)
z =
Dmitry
ans =
6
ans =
D
ans =
y
z2= '1894' % this is a string
length(z2), z2(1), z2(4) % z2 is array of four characters
length(z1), z1(1) % z1 is scalar of one numerical value
z3 = str2num(z2) % conversion from string to number
(z1 == z3) % yes, z3 is now the same as z1, i.e. it is the number
z4 = num2str(z1) % conversion from number to string
(z2 == z4) % yes, z4 is now the same as z2, i.e. it is the string
% NB: when array variables are compared, each element is compared
individually
% NB: for comparisons of array variables, they must be of the same size!
z2 = 1894
ans = 4
ans = 1
ans = 4
ans = 1
ans = 1894
z3 = 1894
ans = 1
z4 = 1894
ans = 1 1 1 1
·
disp: displays
numbers, vectors, matrices, and strings
disp([1, 2, 3, 4]) %
[1,2,3,4] is a vector
disp([1, 2; 3, 4]) %
[1,2;3,4] is a matrix
disp('MESSAGE: 1234 is a string') % displays a string
1 2
3 4
1 2
3 4
MESSAGE: 1234 is a string
·
sprintf: creates a string output from any textual and numerical input
str1 = sprintf('The value of pi equals to %6.3f',pi)
str2 = sprintf('The integer part of e = exp(1) equal to
%6.0f',exp(1))
% NB: the total width of the printed number is 6 (empty characters are added)
The value of
pi equals to 3.142
str2 =
The integer part of e = exp(1) equal to 3
·
fprintf: outputs a formatted message and numbers to the user's terminal or to a
file
fprintf('The
value of pi equals to %6.3f\n',pi);
fprintf('The integer part of e = exp(1) equal to %6.0f\n',exp(1));
The value of pi equals to
3.142
The integer part of e = exp(1) equal to 3
·
formats (the same as in C)
x1 =
sprintf('%5.2f',x) % x1 displays 2 digits of the fractional part
% NB: the total width of
the printed value exceeds 5,
% since the integer part of
x has more characters than the format
x2 = sprintf('%5.2e',x) % x2 displays 2 digits after the period
x3 = sprintf('%5.2g',x) % x3 takes the floating point format as
best
x4 = sprintf('%5.0f',x) % x4 displays rounded integer number
123456789.99
x2 =
1.23e+008
x3 =
1.2e+008
x4 =
123456790
·
Special characters:
o
'\n': new-line character
o
'\t': tab character
o
'\r': carriage return character
o
'\\': backslash character
o
'%%': percent character
fprintf('here is an example\n of a ver\ty fu\t\tnny t\t\t\text!');
of a ver y fu nny t ext!
x = 1; y =
12; z = 123; w = 1234;
fprintf('x = %4d,\ty = %4d\nz = %4d,\tw = %4d\n',x,y,z,w);
% NB: useful formatting of data output
z = 123, w = 1234
Reading from and writing to a data file:
·
fileID = fopen('filename','w'): open the file "filename" for
writing operations
·
fileID: an integer, called a file
identifier (unique in the given working space)
·
permissions:
o
'r' - read
o
'w' -
write (create if necessary)
o
'a' - append (create if necessary)
o
'r+' - read and write (do not create)
o
'w+' - truncate or create for read
and write
o
'a+' - read and append (create if necessary)
·
fclose('fileID'): close the file with the file identifier "fileID" after
all operations
·
fprintf(fileID,'textname'): write the text "textname" into the
file with the file identifier "fileID"
·
fwrite(fileID,variablename): write the variable "variablename"
into the file
·
fscanf, fread: read MATLAB instructions for reading operations
Saving and loading the workspace:
·
save: saving
MATLAB current working space into double precision binary
·
load: retrieving
all saved variables from the file into MATLAB current working space
Note: If multiple variables in different data forms are to be loaded, they have to be prepared in separate ASCII data files! There is no such trouble in binary format: all stored variables are retrived separately and stored into different variables that keep the names of saved variables!
x = [ 1, 2,
3, 4 ]; y = [ -1 ; -2 ];
save data1 x y
clear all;
load data1;
x
y
1 2
3 4
y =
-1
-2