Program MatLab
Lecturer : Leng Por
Email:
[email protected] Phone : +855016955956
7/7/2021 1
Script Files (M-files)
7/7/2021 2
Content
I- Input to a script file
1- The variable is defined and assigned value in the script file.
2- The variable is defined and assigned value in the window
command.
3- The variable is defined in the script file, but a specific value is entered in
the Command Window when the script file is executed:
II- Output commands
1- The disp command
2- The fprintf Command
7/7/2021 3
I- Input to a script file
To run the M-file, you have to save m-file. Then in the command
widow, you have to type the name that u saved (the same name as the
saved file).
1- The variable is defined and assigned value in the script file.
The following is an example of such a case. The script file (saved as
Example) calculates the average point scored in three games.
7/7/2021 4
% this script file calculates the average points scored in three games.
% the assignment of the values of the points is part of the script file.
game1=75;
game2=93;
game3=68;
ave_point=(game1+game2+game3)/3
>> Example
7/7/2021 5
ave_point =
78.6667
2- The variable is defined and assigned value in the window
command.
For the previous example in which the script file has a program that
calculates the average of points scored in three games, the script file
(saved as Example) is:
% The script file calculates the average points scored in the three
games.
% The assignment of the values of the points to the variables.
% game1, game2, and game3 is done in the Command Window.
7/7/2021 6
ave_point=(game1+game2+game3)/3;
The command window for running this file is:
>> game1=67;
>> game2=90;
>> game3=81;
>> Example1
ave_point = 79.3333
7/7/2021 7
3- The variable is defined in the script file, but a specific value is entered
in the Command Window when the script file is executed:
• The form of the input command is:
value_name=input (‘string with a message that is
displayed in the Command
Window’)
• % This script file calculates the average of points scored in three games.
• % The point from each game are assigned to the variable by using the input
• % command.
7/7/2021 8
• game1=input('Enter the point scored in the first game ');
• game2=input('Enter the point scored in the second game ');
• game3=input('Enter the point scored in the third game ');
ave_point=(game1+game2+game3)/3
The following shows the Command Window when this script file (saved
as
• Example2) >> Example2
• Enter the point scored in the first game 67
• Enter the point scored in the second game 91
• Enter the point scored in the third game 70
• ave_point = 76
7/7/2021 9
• The second way is to use an option in the input command that
defines the characters that are entered as a string. The form of the
command is:
value_name=input (‘prompt message’,’s’)
• % This script file calculates the average of points scored in three
games.
• % The point from each game are assigned to the variable by using the
input
• % command.
7/7/2021 10
• game1=input('Enter the point scored in the first game ','s');
• game2=input('Enter the point scored in the second game ','s');
• game3=input('Enter the point scored in the third game ','s');
• ave_point=(game1+game2+game3)/3;
>> Example2a
Enter the point scored in the first game 67
Enter the point scored in the second game 91
Enter the point scored in the third game 70
ave_point =
55.3333 50.6667
7/7/2021 11
II- Output commands
• 1- The disp command
The disp command is used to display the elements of a variable
without displaying the name of the variable, and to display text. The
form of the disp command is:
disp (name of a variable‘’) or disp(‘text as string’)
Ex: >> abc=[5 9 1; 7 2 4];
>> disp(abc)=5 9 1
724
7/7/2021 12
• The next example shows the use of the disp command in the script
file that calculates the average points scored in three games.
• % This script file calculates the average of points scored in three
games.
• % The point from each game are assigned to the variable by using the
input
• % command.
• % The disp command is used to display the output.
• game1=input('Enter the point scored in the first game ');
• game2=input('Enter the point scored in the second game ');
• game3=input('Enter the point scored in the third game ');
7/7/2021 13
ave_points=(game1+game2+game3)/3;
disp('')
disp('The average of points scored in a game is: ')
disp('')
disp(ave_points)
When this file (saved as Example3) is executed, the display in the Command
Window is:
>> Example3
Enter the point scored in the first game 67
Enter the point scored in the second game 91
Enter the point scored in the third game 70
The average of points scored in a game is: 76
7/7/2021 14
• Only one variable can be displayed in a disp command. If elements of
two variables need to be displayed together, a row variable (that
contains the elements to be displayed) must first be defined and then
displayed.
• yr=[1984 1986 1988 1990 1992 1994 1996];
• pop=[127 130 136 145 158 178 211];
• tableYP(:,1)=yr';
• tableYP(:,2)=pop';
• disp(' YEAR POPULATION')
• disp(' MILLIONS')
• disp(tableYP)
7/7/2021 15
• When this script file (saved as PopTable) is executed the display in the
Command Window is:
>> PopTable
YEAR POPULATION
MILLIONS
1984 127
1986 130
1988 136
1990 145
1992 158
1994 178
1996 211
7/7/2021 16
2- The fprintf Command
• The fprintf command can be used to display output (text and data) on
the screen or to save it to a file. With this command (unlike with the
disp command) the output can be formatted. For example, text or
numerical values of variables can be intermixed and displayed in the
same line. In addition, the format of the numbers can be controlled.
7/7/2021 17
Using the fprintf command to display text:
• To display text, the fprintf command has the form:
fprintf(‘text typed in as a string’)
For example:
>> fprintf('The problem, as entered, has no solution. Please check the
input data.')
The problem, as entered, has no solution. Please check the input
data.>>
7/7/2021 18
• With the fprintf command it is possible to start a new line in the
middle of the string. This is done by inserting \n before the character
that will start the new line. For example,
>> fprintf('The proble, as entered, has no solution. Please check the
input data.\n')
The proble, as entered, has no solution. Please check the input data.
>> fprintf('The proble, as entered, has no solution. \nPlease check the
input data.\n')
The proble, as entered, has no solution.
Please check the input data.
7/7/2021 19
• The \n is called an escape character. It is used to control the display.
Other escape characters that can be inserted within the string are:
• \b Back space
• \t Horizontal tab
• When a program has more than one fprintf command, the display
that they generate is continuous (the fprintf command does not
automatically start a new line).
• This is true even if there are other commands between the fprintf
commands. An example is the following script file:
7/7/2021 20
• fprintf('The problem, as entered, has no solution. Please check the input data.')
• x=6; d=19+5*x;
• fprintf('Try to run the program later.')
• y=d+x;
• fprintf('Use different input values.')
• >> Example4
• The problem, as entered, has no solution. Please check the input data. Try to run
the
• program later. Use different input values.
• fprintf('The problem, as entered, has no solution. Please check the input data.')
• x=6, d=19+5*x
• fprintf('Try to run the program later.')
• y=d+x
• fprintf('Use different input values.')
7/7/2021 21
• >> Example4a
• The problem, as entered, has no solution. Please check the input data.
x=6
d = 49
• Try to run the program later.
• y = 55
• Use different input values.
• To start a new line with the fprintf command, a \n must be typed at the
start of the string.
>> Example4b
The proble, as entered, has no solution. Please check the input data.
x = 6, d=49
7/7/2021 22
• Try to run the program later.
•y=
• 55
• Use different input values.
• Using the fprintf command to display a mix of text and numerical
data:
• - To display a mix of text and a number (value of a variable), the
fprintf
• command has the form:
7/7/2021 23
7/7/2021 24
• The field width and precision (1 in the previous example) are optional.
The first number (5 in the example) is the field width that specifies
the minimum number of digits in the display. If the number to be
displayed is shorter than the field width, spaces or zero are added in
front of the number. The precision is the second number (2 in the
previous example) specifies the number of digits to be displayed to
the right of the decimal point.
7/7/2021 25
The last element in the formatting elements, which is required, is the
conversion character which specifies the notation in which the
number is displayed. Some of the common notations are:
7/7/2021 26
• Information about additional notation is available in the help menu of
MATLAB.
• As an example, the fprintf command with a mix of text and a number is
used in the script file that calculates the average points scored in three
games.
• % This script file calculates the average of points scored in three games.
• % The point from each game are assigned to the variable by using the input
command.
• % The fprintf command is used to display the output.
7/7/2021 27
• game(1)=input('Enter the point scored in the first game ');
• game(2)=input('Enter the point scored in the second game ');
• game(3)=input('Enter the point scored in the third game ');
• ave_points=mean(game);
• fprintf('An average of %f points was scored in the three
games.\n',ave_points)
• The command window where the script file above (saved as
Example5) was run is shown below.
7/7/2021 28
• >> Example5
• Enter the point scored in the first game 75
• Enter the point scored in the second game 60
• Enter the point scored in the third game 81
• An average of 72.000000 points was scored in the three games.
The display generated by the fprintf command
combines text and a number (value of a variable).
7/7/2021 29
• With the fprintf command it is possible to insert more than one
number (values of a variable) within the text. This is done by typing
%g (or % followed by any formatting elements) at the places in the
text where the numbers are to be inserted. Then, after the string
argument of the command (following the comma), the name of the
variables are typed in the order that they are inserted in the text. In
general the command looks like:
7/7/2021 30
• fprintf(‘…text…%g…%g…%f…’,variable1,variable2,variable3)
• An example is shown in the following script file:
• % This program calculates the distance a projectile flies, given its initial
velocity and the angle it is shot.
• % the fprintf command is used to display a mix of text and numbers.
• v=1584; % Initial velocity (km/h)
• theta=30; % Angle (degrees)
• vms=v*1000/3600; % Changing velocity units to m/s.
• t=vms*sin(30*pi/180)/9.81; % Calculating the time to highest point.
7/7/2021 31
• d=vms*cos(30*pi/180)*2*t/1000; % Calculating max distance.
• fprintf('A projectile shot at %3.2f degrees with a velocity of %4.2f km/h will
travel a
• distance of %g km.\n',theta,v,d)
• When this script file (saved as Example6) is executed, the display in the
Command Window is:
• >> Example6
• A projectile shot at 30.00 degrees with a velocity of 1584.00 km/h will
travel a distance of 17.091 km.
7/7/2021 32
Additional Remark About the fprintf
Command:
• To have a single quotation mark in the displayed text, type two single
quotation marks in the string inside the command.
• - The fprintf command is vectorized. This means that when a variable
that is a vector or a matrix is included in the command, the command
repeats itself until all the elements are displayed. If the variable is a
matrix the data is used column by column.
7/7/2021 33
• To have a single quotation mark in the displayed text, type two single
quotation marks in the string inside the command.
• - The fprintf command is vectorized. This means that when a variable
that is a vector or a matrix is included in the command, the command
repeats itself until all the elements are displayed. If the variable is a
matrix the data is used column by column.
7/7/2021 34
• When the script file is executed the display in the Command Window
is:
• >> Example7
•T=
• 1.0000 2.0000 3.0000 4.0000 5.0000
• 1.0000 1.4142 1.7321 2.0000 2.2361
• If the number is: 1, its square root is: 1.000000
• If the number is: 2, its square root is: 1.414214
• If the number is: 3, its square root is: 1.732051
• If the number is: 4, its square root is: 2.000000
• If the number is: 5, its square root is: 2.236068
7/7/2021 35
• Note: the special use of the fprintf
• % The program calculates the coordinates of the centroid of a
coposite area.
• clear C xs ys As
• C=input('Enter a matrix in which each row has three elements.\nIn
each row enter the x and y coordinates of the centroid and the area
of a section.\n');
• xs=C(:,1)'; % Creating a row vector for the x coordinate of each
section (first column of C).
• ys=C(:,2)'; % Creating a row vector for the y coordinate of each
section (second column of C).
7/7/2021 36
• As=C(:,3)'; % Creating a row vector for the area of each section (thirst
column of C).
• A=sum(As); % Calculating the total area.
• x=sum(As.*xs)/A; % Calculating the coordinates of the centroid of the
composite area.
• y=sum(As.*ys)/A;
• fprintf('The coordinates of the centroid are: (%f,%f)\n',x,y)
• -The script file was saved with the name Centroid. The following shows the
Command Window where the script file was executed.
7/7/2021 37
• >> Example8
• Enter a matrix in which each row has three elements.
• In each row enter the x and y coordinates of the centroid and the
area of a section.
• [100 100 200*200; 60-120/pi 200+120/pi pi*60^2/4; 60+140/3 220
140*60/2; 100 100/pi
• -pi*50^2/2; 150 95 -40*150; 105 145 -50*50]
• The coordinates of the centroid are: (85.387547,131.211809)
7/7/2021 38
7/7/2021 39