35
1 Input / Output ut – reads/gets data for the program put – the product, after processing oth can be: nteractive I/O (while program is running) (RAM memory) r file I/O: (read/write to file) (Hard disk Memory)

1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

Embed Size (px)

Citation preview

Page 1: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

1

Input / Output

Input – reads/gets data for the programOutput – the product, after processing

Both can be: interactive I/O

(while program is running)(RAM memory)

or file I/O:(read/write to file)(Hard disk Memory)

Page 2: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

2

Interactive Input

Built-in functions for interactive input

•input•menu•keyboard •(pause)

Page 3: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

3

input:

input(‘string’) displays the text in string on the screen, and waits for the user to give a keyboard input

>> n = input('How many years? ' ) % prompts the user to input an integer number,

% and saves it in n

We will see on the command window: How many years?

Page 4: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

4

input:

input(‘string’) displays the text in string on the screen, and waits for the user to give a keyboard input

>> n = input('How many years? ' ) % prompts the user to input an integer number,

% and saves it in n

We will see on the command window: How many years? 12n = 12

Page 5: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

5

Input (cont.):

The input may also be a string

>> more = input('More Simulations ? (y/n): ' , 's' ) % Prompts the user to type "y” for % yes or “n” for no, and stores the % input as a string in “more”.

The second argument, “s”, of the command directs MATLAB to save the user input as a string

Page 6: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

6

>> more = 'y';>> while (more == 'y') disp('eat') more = input('Are you hungry?','s')endeatAre you hungry?more =yeatAre you hungry?more =yeatAre you hungry?more =yeatAre you hungry?more =n>>

y

y

y

y

Example

Page 7: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

7

menu:

menu(‘name’,’option1’,’option2’,..)

creates an on-screen menu named as we requested in ‘name’, with several options

The user selects any option using the mouse

Page 8: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

8

PopOption=menu(‘Initial size','200', '250','300','350')

Page 9: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

9

PopInitials=[200, 250, 300, 350];

Pop=PopInitials(PopOption)

Pop=

350

PopOption=menu(‘Initial size','200', '250','300','350')

PopOption = 4

Page 10: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

10

Interactive OutputWe have seen some already

by naming a variable, without ; it shows on output window:

if we write A

we get the whole matrix

A=

1 2 3…

if we write A(10,:)

we get

ans=

10 20 30 40 50 60 70 80 90 100

If we don’t want to see the ans= , we use the function disp

Page 11: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

11

disp(x)

displays whatever is in the parentheses

It can be anything - a string, an integer, a real number, a vector, an array

x= 'A string needs to be between single quotes' ; y = 4; z=6.8; u =[2 3 4 6];

>> disp(x); disp(y); disp(z); disp(u) A string needs to be between single quotes

4

6.8000

2 3 4 6

Page 12: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

12

plot(x,y)

Plots y as a function of xOften very useful to display results

Example from your program Basics: Pop =

1.0000 240.0000 48.0000 30.0000 18.0000 2.0000 258.0000 51.6000 32.2500 19.3500 3.0000 277.3500 55.4700 34.6688 20.8013 4.0000 298.1513 59.6303 37.2689 22.3613 5.0000 320.5126 64.1025 40.0641 24.0384 6.0000 344.5510 68.9102 43.0689 25.8413 7.0000 370.3924 74.0785 46.2990 27.7794 8.0000 398.1718 79.6344 49.7715 29.8629 9.0000 428.0347 0 0 0 10.0000 0 0 0 0

Page 13: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

13

>>plot(Pop(:,1),Pop(:,2))

Page 14: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

14

File Input/Output Reading-from and writing-to a file

SaveLoad

fopenfclose

fprintffscanf frewind

Writing and reading MATLAB data

General text files

Page 15: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

15

save(‘fileName’)

•Saves your workspace (all variables defined in the Command Window) in a file named filename.mat•When called within a function, saves all the variables of defined within.

load(‘fileName’)

•Restores the saved variables.`

Page 16: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

16

save('fileName',varName)

•Saves the variable “varName” in a file named filename.mat

Page 17: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

17

fopen:

Before we use a file (for reading or writing) - must be opened with this function

The function returns an integer number, which is used asa file identifier so that other functions can access this file.

Page 18: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

18

fid = fopen ('filename.txt', 'r')

opens a file called filename.txt for reading of dataAssigns an id-number to it in the backgroundfor us, the file is referred to as ‘fid’

File handle File name Permission mode

Permission modes:•‘r’ – read•‘w’ – write (overwrite)•‘a’ – write (append)

Page 19: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

19

If the opening operation fails (e.g., you ask to open for reading a non-existing file), the function returns -1 as the value behind the handle, so you know there is problem

f1=fopen('input6.txt','r')

f1 =

-1

27

Page 20: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

20

fprintf

used to write formatted data into a file

The structure of this function is:fprintf(fid, FORMAT string, list of variables)

fid: the file identifier, i.e., the handle of the open file, on which we write

Page 21: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

21

fprintf(fid, FORMAT string, list of variables)

FORMAT string: a string (enclosed in ‘ ‘) containing conversion specifications

Each specifier in this string is headed by the character “%”

There are a number or different specifiers-

Page 22: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

22

fprintf(fid, FORMAT string, list of variables)

FORMAT string: a string (enclosed in ‘ ‘) containing conversion specifications

fid = fopen ('filename.txt', 'w');fprintf(fid,'The answer is %d not %f\n',42,1.5);

Creates a new file with a single line

The answer is 42 not 1.500000

Page 23: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

23

fprintf(fid,'The answer is %d not %f\n',42,1.5);

Specifier (integer) Conversion specification(floating point)

Special character(new line)

Page 24: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

24

Any scalar can be written in different forms: a string, an integer, a real number, a character

etc. For example, the number 67 can be written

as-

Page 25: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

25

Specifier output meaning

%d 67 (an integer) %10d 67 (an integer shifted 10 places to the right)%f 67.000000 (a real number with 6 digits after the decimal point)%.1f 67.0 (a real number with 1 digit after the decimal point)%.3f 67.000 ( a real number with 3 digits after the decimal point)%10.3f 67.000 ( a real number with 3 digits after the decimal point,

and the whole number takes 10 spaces)%c C (a character , ASCII code of 67) %e 6.700000e+001 (a real number in scientific notation)

Hence:fprintf(f1,'%d %5d %f %.1f %.3f %8.3f ', 67,67,67, 67, 67, 67)

67 67 67.000000 67.0 67.000 67.000

Page 26: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

26

x = 0:.2:1fid1 = fopen('test.txt','w');fprintf(fid1,'%6.2f \n',x);

Produces on the screenx =

0 0.2000 0.4000 0.6000 0.8000 1.0000But produces in a file named “test.txt” due to the \n:

0.000.20 0.400.600.801.00

Writing vectors to file

Page 27: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

27

>> x = 0:.2:1x = 0 0.2000 0.4000 0.6000 0.8000 1.0000>> y = [x ; exp(x)]y = 0 0.2000 0.4000 0.6000 0.8000 1.0000 1.0000 1.2214 1.4918 1.8221 2.2255 2.7183

Writing an array to a file

Page 28: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

28

>> fprintf(fid,'%6.2f %6.3f\n',y');Writes to the file0.00 1.000 0.20 1.221 0.40 1.492 0.60 1.822 0.80 2.226 1.00 2.718

Page 29: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

29

fclose

A file on which we wrote must be closed to see its new contents

A file from which we read must be closed and reopened (or rewind) to read again from its beginning fclose(fid)

closes the file associated with the identifier “fid”. The function returns 0 if successful and -1 if not. fclose('all') % closes all open files

Page 30: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

30

Example:

%Temptable- generates and writes a temperature % table script file to generate a Fahrenheit-Celsius % temperature table. The table is written in a file % named ‘Temperature.table’

F=-40:5:100C=(F-32)*5/9t=[F;C];fid=fopen('Temperature.table','w');fprintf(fid,' Temperature Table\n');fprintf(fid,' ________________\n');fprintf(fid,' Fahrenheit Celsius\n');fprintf(fid,' %4i %15.2f\n',t)fclose(fid);

Page 31: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

31

Temperature Table ________________

Fahrenheit Celsius -40 -40.00 -35 -37.22 -30 -34.44 -25 -31.67 -20 -28.89 -15 -26.11 -10 -23.33 -5 -20.56 0 -17.78 5 -15.00 10 -12.22 15 -9.44 20 -6.67 25 -3.89 30 -1.11 35 1.67 40 4.44 45 7.22 50 10.00 55 12.78 60 15.56 65 18.33 70 21.11 75 23.89 80 26.67 85 29.44 90 32.22

95 35.00.

When we open the file“Temperature.table” we find

Page 32: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

32

fscanf

A = fscanf(fid, FORMAT, SIZE)

Reads into A formatted data from file “fid”

Converts it according to FORMAT string

The format string is recycled through the file until an end-of-file is reached or the amount of data specified by SIZE is read in.

Page 33: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

33

x = 0:.2:1; y = [x; exp(x)]; % Here we created it fid = fopen('exp.txt','w'); fprintf(fid,'%6.2f %6.3f\n',y); fclose(fid);

fid=fopen('exp.txt','r') % open it again for reading

% read from itA=fscanf(fid,'%f %f',[2,inf]) % 2 rows only , whole length% inf can be used only in the second dimension

A = 0 0.2000 0.4000 0.6000 0.8000 1.0000 1.0000 1.2210 1.4920 1.8220 2.2260 2.7180

Example – Let’s open and read from file we recently wrote on:

Page 34: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

34

fclose(fid); % close and open the file againfid=fopen('exp.txt','r')

% Now asking just for 2 rows and 3 columns

A=fscanf(fid,'%f %f',[2,3])

A =

0 0.2000 0.4000 1.0000 1.2210 1.4920

Page 35: 1 Input / Output Input – reads/gets data for the program Output – the product, after processing Both can be: interactive I/O (while program is running)

35

frewind

frewind(fid)

Sets the file position indicator to the beginning of the file “fid”

Useful if we need to read same file again Replaces closing and opening again