LECTURE 2 Using Script Files and Managing Datame313.cankaya.edu.tr/uploads/files/LECTURE 2...
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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/1.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/3.jpg)
![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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/4.jpg)
>> 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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/6.jpg)
![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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/7.jpg)
![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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/10.jpg)
![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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/11.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/12.jpg)
![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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/13.jpg)
![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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/15.jpg)
![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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/17.jpg)
![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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/18.jpg)
![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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/19.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/20.jpg)
![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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/21.jpg)
% 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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/22.jpg)
![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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/23.jpg)
% 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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/24.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/25.jpg)
>> 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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/26.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/27.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/28.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/29.jpg)
>> 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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/30.jpg)
>> 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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/31.jpg)
>> 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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/32.jpg)
![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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/33.jpg)
![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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/34.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/35.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/36.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/37.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/38.jpg)
![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](https://reader033.fdocuments.net/reader033/viewer/2022052612/5f0ee1837e708231d44164e2/html5/thumbnails/39.jpg)