This part of the tutorial deals with Script files and Functions.

 

Script Files

 

1) For Simple problems requests can be typed in the command window and executed .The process is also

fast and efficient .However as the number of commands increases or any changes have to be made to the variables in the program , the process of typing the commands at the command prompt can be tedious. To overcome this problem matlab allows us to type all the commands in a file and execute at one time like a batch file.These files are called Script Files or M-Files.

 

2) M-Files have an extension of m for example your file name could be example.m .

 

3) To create a new M-File choose New from File Menu and then select M-File .

Another alternative is to type the commands in any editor such as Notepad and then save it with .m as

the extension(recommended) or it may not have .m as extension but the file should be a text file.

 

4) Now Type the matlab commands as if you were typing at the command prompt and save the file.

5) To execute the script file choose "Run script" option from File Menu and choose the Script file you want

execute .Another way of executing the script is to type the name of the file at the command prompt and

executing it.Before executing the Script File one must set the path of the Directory. To set the path of

directory choose "Set Path " option from the File Menu and select the directory in which the script file

is present .Typically the matlab searches all the files in the path for the file and opens it and executes it

as if the commands are entered at the command prompt.

 

6) Example :

% Script File Example

books = 4 ; pens = 2;

cost = books * 2.5 + pens * .25

Save the file as script.m and execute it,the result i.e. cost is displayed at the command is displayed.

Note:

typed at the command prompt.

 

7) Some Utility functions which can be used in script files.

Function

Description

Disp(variable)

Displays result without identifying the variables.

Echo[on or off]

Control the command window echoing the script file commands

Input

Prompt input from the user.

e.g books = input(‘Enter Number of books>’);

Keyboard

Give the control to keyboard temporarily .Type return(at command) to return control to the executing script file.

Pause

Pause(n)

Pause until user presses any keyboard key.

Pause for n seconds ,then continue

Waitforbuttonpress

Pause until user presses mouse button or keyboard key.

 

Note:

 

 

 

FUNCTIONS

 

 

1)When matlab functions such as angle ,sqrt are used matlab takes the variables that we supply to it process it and sends us back the results of the operation.In the process the commands evaluated by the function and the variables used in the intermediate process are hidden .Similarly we can write user defined functions and use them .

 

Example: You may want write a function called "integrate" which integrates a function passed to it and sends the result .

 

2)A Function M-File is similar to a script file ,But it should have an extension of .m .The intermediate variables of the function do not appear or interact in the matlab workspace.

 

Example:

function y = flipud(x)

% The Function flipud flips the matrix up/down.

% flipud(x) returns x with columns preserved and rows flipped

% in the up/down direction .For Example

% x = 1 4 becomes 3 6

% 2 5 2 5

% 3 6 1 4

%

% This Portion of the comment after the function declaration appears as the help when

% help flipud is typed at the command prompt

% This is a very good feature of matlab as you can easily provide for the help for function with out much

% difficulty .Also good feature for Documentation.

if ndims(x) ~= 2, error(‘x must be a 2-D matrix.’);end

[m,n] = size(x);

y = x(m:-1:1,: );

 

M- FILE Construction Rules:

 

  1. The Function name(appears in the first line of the function) and the file name of the function should be identical.In reality matlab executes the functions based on the file names stored on the disk.
  2. Function names must begin with a letter and then any combination of the letters ,numbers and underscores can be used.The maximum length of the file should not be more than 31 characters.
  3. The first line of the M-File is called the function declaration line and must contain the word function
  4. And then followed by the calling syntax for the function in the most general form.The input and output variables identified in the first line are local to the function.The input variables are used to pass the values to the function and output variables contain data passed back from the function.

  5. The first set of comment lines appear as help when "help function_name" is typed at the command
  6. Line.

  7. All Statements after the contiguous comment lines compose the body of the function.
  8. A function M-File terminates after the last line in the file is executed or whenever a return statement is encountered.
  9. A function can abort an operation and return control to the command window by calling the function "error" .

e.g. if length(val)>1

error (‘Val must be a scalar’)

end

  1. Similarly a function called "warning" can be used to display a text message on the command window.

The processing of the function continues after warning is displayed on the command window.

Global messages warning on and warning off can be declared to turn all the warnings on or off.

  1. A Function may also call a Script file(provided the file is in the path).
  2. Multiple functions can exist in a single M-File .Additional functions are called Sub Functions or local
  3. Functions.They are simply appended at the end of the primary function.Sub Function begin with a standard function statement line and follow function construction rules. The first function in the M-file is the pimary function.

  4. Sub functions can be called from the Primary function of the M-file or by any other sub function in the same M-file . They cannot be called from other functions .Help text is not displayed for sub function for help command.

 

 

INPUT AND OUTPUT Arguments:

 

a)Matlab functions may have any number any number of input and output arguments including zero.

b)Functions cannot be called with more number of input or output arguments than the M-File declaration.

c)The number of input and output arguments used in a function calls to the functions nargin and nargout.

d)If a function declares one or more output arguments ,but no output is desired ,simply do not assign values to the output variables or variable.