Tuesday, March 21, 2017

                                                              Chapter-3
                                                    SCRIPT FILES
So far in this book, all the commands were executed in the command window.
A different way of executing commands with MATLAB is first to create a program with a list of commands, save it and then run the program. The files or programs that are used for this purpose are called script file or m-file (because .m is used when they are saved).
In MATLAB script files are created and edited in the Editor window. For opening new m-file, In the File menu, select New and then select M-file or press ctrl+N. The names of user-defined variables, predefined variables, MATLAB commands or functions should not be used to name script file.
 Fig 3.1 shows example of Editor/Debugger window


A script file can be executed either by typing its name in the command window and then pressing the Enter key, or directly from the Editor window by clicking on run icon.
CURRENT DIRECTORY OR CURRENT FOLDER:-
Fig 3.2 shows current directory field in the command window


The assignment of a value to a variable can be done in two ways:
1 Variable can be defined and value can be assigned to the variable in the script file itself. Following script file is an example of such a case. This script file (saved as average run) calculates the average run scored by batsman in three games. After reading this script file we will get an example of how variable can be defined and value can be assigned to the variable in the script file itself.

%script file calculates the average run
clc % to clear screen
clear all % to clear previously defined variable in workspace
run1 = 150;
run2 = 42;
run4 = 32;
average_run = (run1 + run2 + run3)/3;



If we save and run this m-file, the variables that we have defined in this script file will appear in the workspace and we can get value of variables that we have defined here by typing and entering the value of variable in the command window. For example:

>> average_run
average_run =
    74.6667
*link for downloading program: averagerun.m from folder matlabfiles

2 Variable is defined in the script file, but a specific value is entered by user in the command window when the script file is executed. This is done by using input command.

Syntax for using input command
Variable_name = input('message that we want to display in command window')

When input command is executed, the user is prompted to assign a value to the variable in the command window. The user types the value and presses the Enter key.
We have given an example of program (script file saved as average run) that calculates the average run scored by batsman in three games and uses the input command to enter the points scored in each game.

%script file that calculates the average run
%input command will be used
clc % to clear screen
clear all % to clear previously defined variables in workspace
run1 = input('enter runs scored in first game');
run2 = input('enter runs scored in second game');
run3 = input('enter runs scored in third game');
average_run = (run1 + run2 + run3)/3 %statement is without semicolon, value   %of average_run will be displayed in command window


*link for downloading program: averagerun2.m from folder basicmatlab

>>averagerun
enter runs scored in first game 150
enter runs scored in second game 42
enter runs scored in third game 32
average_run =
    74.6667

We have saved above program as averagerun.m and we can run above program by typing averagerun in command window. After running this m-file we have understood about input command.

THE disp COMMAND:
The disp command can be used to display the elements of a variable and can also used to display the message.Syntax for using disp command
disp(name of a variable)
or disp(‘message’)
Text and numerical values of variables cannot be intermixed and displayed in same line using disp command.

THE  fprintf COMMAND:
The fprintf command can be used to display output (text and data) on the screen. By using fprintf command we can intermix text and numerical values of variables and display in same line.
Syntax for using fprintf command:
 fprintf(‘message’)
For example:

fprintf('learning MATLAB is easy');

If this line is a part of script file, when the line is executed, the following is displayed in the command window.

learning MATLAB is easy

With the fprintf command it is possible to start a new line in the middle of the message. This is done by inserting \n before the character that will start the new line.

>>fprintf(‘learning MATLAB is easy.\n proper guidance and good content is required’);

After writing this line in command window, if we press enter key, following will be displayed in command window.

learning MATLAB is easy.
 proper guidance and good content is required

 USE OF fprintf COMMAND TO DISPLAY A MIX OF TEXT AND NUMERICAL VALUE:


Syntax for using fprintf command to display a mix of text and numerical data:
Following script file uses fprintf command to display a mix of text and numerical value. Following script file uses fprintf command to display a mix of text and numerical value. Following program calculates average of runs scored by batsman in three matches.

%script file that calculates the average run
clc % to clear screen
clear all % to clear previously defined variables in workspace
run1 = 110;
run2 = 60;
run3 = 9;
average_run = (run1 + run2 + run3)/3;
fprintf('average of runs scored in three games is %f',average_run);


Output of command in command window:

average of runs scored in three games is 59.666667

f in %f is the conversion character which specifies the notation in which number is displayed.

LIST OF COMMON CONVERSION CHARACTER IN MATLAB

 e
Exponential notation using lower case e(e.g 1.709e+001)
 f
Fixed point notation (e.g 17.09)
 g
The shorter of e or f notation

With the fprintf command it is possible to insert more than one variable within the text.

Syntax for inserting more than one variable within the text:

fprintf(‘----text--%f---%f--------%f’,variable1,variable2,variable3);

following program calculates the horizontal distance covered by projectile:

%program showing that multiple variables can be printed using fprintf command
clc
clear all
u = 30;%velocity with which projectile was thrown
theta = 45*(pi/180);
g = 9.8;
horizontaldistance = (u^2*sin(2*theta))/g;
fprintf('projectile fired with a velocity of %fm/s at an angle of %f radians will attain a horizontal distance of %f m',u,theta,horizontaldistance);

*link for downloading program: projectile.m from folder basicmatlab

Result of m-file

projectile fired with a velocity of 30.000000m/s at an angle of 0.785398 radians will attain a horizontal distance of 91.836735 m



No comments:

Post a Comment