Lecture 6

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

1

Lecture 6: Scripts and User Functions

Lecture 6: Scripts and User Functions


Files that contain code in the MATLAB language are called M-files. You create
M-files using a text editor, then use it as you would any other MATLAB function or
command.
There are two kinds of M-files: scripts and user functions.
6.1 Scripts:
The special significances of a script file are that:
1) Scripts M-file does not accept input arguments or return output arguments. They
operate on data in the workspace.
2) The rules for script file names are the same as those for MATLAB variable
names.
3) If you enter its name at the command-line prompt, MATLAB carries out
each statement in it as if it were entered at the prompt.
You need to save the program if you want to use it again later. To save the
contents of the Editor, select Save from the Editor menu bar. Under Save file as, select
a directory and enter a filename, which must have the extension .m, in the File name
box (for example, Prog1.m). Click Save. The Editor window now has the title Prog1.m.
If you make subsequent changes to Prog1.m an asterisk appears next to its name at the
top of the Editor until you save the changes.
When you invoke a script, MATLAB simply executes the commands found in
the file. Scripts can operate on existing data in the workspace, or they can create new
data on which to operate. Although scripts do not return output arguments, any
variables that they create remain in the workspace, to be used in subsequent
computations. In addition, scripts can produce graphical output using functions like
plot.
6.1.1 Input to a Script File:
When a script file is executed, the variables that are used in the calculations
within the file must have assigned values. The assignment of a value to a variable can
be done in three ways.

Programming II 2nd Year Rasha Hassan


2
Lecture 6: Scripts and User Functions

❖ The variable is defined in the script file.


❖ The variable is defined in the command prompt.
❖ The variable is entered when the script is executed.
Here, we will focus our attention on the third one. In this case, the variable is
defined in the script file. When the file is executed, the user is prompted to assign a
value to the variable in the command prompt. This is done by using the input
command.
input: displays the prompt string on the screen, waits for input from the keyboard.
6.1.2 Output Commands:
As discussed before, MATLAB automatically generates a display when
commands are executed. In addition to this automatic display, MATLAB has several
commands that can be used to generate displays or outputs.
Two commands that are frequently used to generate output are: disp and fprintf.
The main differences between these two commands can be summarized as follows:
disp: • Simple to use.
• Provide limited control over the appearance of output.
fprintf: • Slightly more complicated than disp.
• Provide total control over the appearance of output.

For example, we begin by entering and running the code:

Programming II 2nd Year Rasha Hassan


3
Lecture 6: Scripts and User Functions

6.2 User Functions:


Some of the functions, like sqrt, sin, and other functions, mentioned later, are
built-in. They are part of the MATLAB core so they are very efficient, but you may
want to implement your only function in M-files and even modify it if you want. The
form that you use it to build your only function in M-file is:
function [outorg1, outorg2, …] = function_name (inarg1, inarg2, …)
% comments to be displayed with help
outorg1 = … ;
outorg2 = … ;
.
.
.
end
inarg1, inarg2,... are the input variables to the function filename
outarg1, outarg2,... are the output variables from the function filename
function The function file must start with the keyword function (in the function
definition line).
Note that the function name will be the M-file name.
Functions can accept input arguments and return output arguments. Internal
variables are local to the function.
Programming II 2nd Year Rasha Hassan
4
Lecture 6: Scripts and User Functions

For example, we begin by entering and running the code:

Example 1: Write a code script which enables a user to input the coefficients of a
quadratic 𝑞(𝑥) = 𝑎𝑥 2 + 𝑏𝑥 + 𝑐 , where 𝑎 = 2, 𝑏 = 3, 𝑐 = 7, and 𝑥 = 1.
Solution:

Programming II 2nd Year Rasha Hassan


5
Lecture 6: Scripts and User Functions

Example 2: Write a user function to calculate f(x) which defined by:


𝑥 −𝜋 𝜋
𝑓(𝑥) = 𝑤ℎ𝑒𝑟𝑒 𝑥 = [ , ]
1 + 𝑥2 2 2
Solution:

Programming II 2nd Year Rasha Hassan


6
Lecture 6: Scripts and User Functions

Example 3: Write a code script which enables a user to calculate the time related
function 𝑓(𝑡) = 4 − 𝑡 2 𝑒 −3𝑡 with time range 0 ≤ 𝑡 ≤ 3 seconds.
Solution:

Programming II 2nd Year Rasha Hassan


7
Lecture 6: Scripts and User Functions

Programming II 2nd Year Rasha Hassan

You might also like