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

• for a numerical input: z = input('string'), where 'string' is a prompting message

Enter radius of a sphere: 5

r =

5

• for a textual input: z = input('string','s'), where z is the string for input text

z = input('Enter your name: ','s')

length(z)

z(1)

z(6)

z =

Dmitry

ans =

6

ans =

D

ans =

y

• for general use: z = input('string'), where type variable for numerical input and 'variable' for string input

z1 = 1894 % this is number

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!

z1 =     1894

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(1234) % 1234 is a number

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

1234

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)

str1 =

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)

• "%10.2f" – fixed point format
• "%10.2e" – floating point format
• "%10.2g" – the best of fixed or floating point format
• "%10.0f" – fixed point format for a rounded integer part of a number
• "%d" – displays an integer
• "%s" – display a string

x = 123456789.987654321;

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

x1 =

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

here is an example

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

x =    1, y =   12

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        '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

·         save: saving MATLAB current working space into double precision binary

• save – all current variables are saved into file "matlab.mat"
• save filename – all current variables are saved into file "filename.mat"
• save filename <variablename> – only the list of variables in "variablename" is saved into the file "filename.mat" (NB: there is no separation of variables in the list by a comma)
• save filename <variablename> - ascii : the data is saved into ASCII format, the file can be viewed by editing sources

·         load: retrieving all saved variables from the file into MATLAB current working space

• load filename – load all saved variables from the file "filename.mat" (NB: the file must exist either in the same directory or in the valid path)
• load filename – ascii : load all data from the file "filename.mat" into one variable called "filename" (NB: the file "filename.mat" must have data into one of the following forms)
• a single number
• a row vector
• a column vector
• a matrix

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;

x

y

x =

1     2     3     4

y =

-1

-2