LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2...

39
LECTURE 2 Using Script Files and Managing Data

Transcript of LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2...

Page 1: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

LECTURE 2

Using Script Files

and

Managing Data

Page 2: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

Recall that you can perform operations in

MATLAB in two ways:

1. In the interactive mode: all commands are

entered directly in the Command window,

In the script file mode: By running a MATLAB

program stored in script file. This type of file

contains MATLAB commands, so running it is

equivalent to typing all the commands—one at

a time—at the Command window prompt. You

can run the file by typing its name at the

Command window prompt.

Page 3: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This
Page 4: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

>> x=3.0;

>> y=3^x;

>> disp(y)

27

Example

>> disp(' And now for something completely different' )

And now for something completely different

Example

>> disp('----------------------------------');

4

Page 5: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

Example

>> x=input('Enter r=')

Enter r=5

x =

5

Page 6: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This
Page 7: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This
Page 8: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

string.

>> name=input('What is your name ? ' , 's')

What is your name ? Hakan

name =

Hakan

>> name

Hakan

Page 9: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

The fprintf command can be used to display output (text and data) on the screen or to save it to file.The output can be formatted.

>> fprintf('The problem has no solution \n')

This is displayed on the screen. The \n starts a new line.

The problem has no solution

Page 10: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This
Page 11: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

continued

Page 12: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This
Page 13: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This
Page 14: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

Solution save the file as game.m

Page 15: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This
Page 16: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

Example:

Create a 2x5 matrix T in which the first row contains

numbers 1 through 5 and the second row shows the

corresponding square roots

Page 17: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This
Page 18: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This
Page 19: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

Once the file is open ,the fprintf command can be used to write output to the file.The variable fid is inserted inside the command.fprintf command has the form.

Page 20: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This
Page 21: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

% Conversion table from pound force to Newton

% FN-force in Newton( N)

% Flbf-force in pound force(lbf)

Flbf=200:200:2000;

FN=4.448*Flbf;

TABLE=[Flbf;FN];

fid1=fopen('FlbftoFN.txt','w')

fprintf(fid1,'Force Conversion Table \n \n');

fprintf(fid1, ' Poundforce Newtons \n');

fprintf(fid1 , ' %10.2f %10.2f \n' ,TABLE)

fclose(fid1)

Page 22: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This
Page 23: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

% Calculation of capacitance of capacitor

A=0.43;

s=0.005;

fid=fopen('capacitance.txt','w');

% calculate the capacitance

C=0.000008855*A/s;

% print the result to a file

fprintf(fid, 'Capacitance C=%10.5f \n', C );

fclose(fid);

Page 24: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

The save command

save command can also be used for saving only some of the variables that are in workspace.

Page 25: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

>> clear

>> A=magic(3)

>> clear

>> A=magic(3)

A =

8 1 6

3 5 7

4 9 2

>> save data.dat A -ascii

>>

Page 26: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

The load command can be used for retrieving some

Page 27: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

Example

Data shown below is written in a note pad and saved as PumpData.dat

Use load command to retrieve data

Continued on next slide

Page 28: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

600 2

800 6

1000 10

1500 12

2000 14

4000 20

6000 30

8000 40

10000 50

20000 55

30000 57

Page 29: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

>> data=load('PumpData.dat')

data =

600 2

800 6

1000 10

1500 12

2000 14

4000 20

6000 30

8000 40

10000 50

20000 55

30000 57

Page 30: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

>> x=0:0.1:0.4

x =

0 0.1000 0.2000 0.3000 0.4000

>> y=exp(x)

y =

1.0000 1.1052 1.2214 1.3499 1.4918

>> T=[x;y]

T =

0 0.1000 0.2000 0.3000 0.4000

1.0000 1.1052 1.2214 1.3499 1.4918

>> save TABLE.dat T -ascii

Page 31: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

>> data=load('TABLE.dat')

data =

0 0.1000 0.2000 0.3000 0.4000

1.0000 1.1052 1.2214 1.3499 1.4918

>>

We can also use Import Wizard to retrieve data

Suppose we want to import the data in PumpData.dat file

Page 32: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This
Page 33: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This
Page 34: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

27

Page 35: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

35

Page 36: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

diary

Diary command saves your input to MATLAB and most

of the output to disk. This command toggles diary on and

off. (If no file is given, it is saved to the file diary in the

current directory.)

diary on turns the diary on.

diary off turns the diary off.

36

Page 37: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This

1)

Page 38: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This
Page 39: LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2 Using...In the script file mode: By running a MATLAB program stored in script file. This