0% found this document useful (0 votes)
5 views18 pages

MATLAB Module 5

notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views18 pages

MATLAB Module 5

notes
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

A

Module 5

D
AD
Programming in MATLAB

U
VT
Script Files
• A script file is an M-file with a set of valid MATLAB commands in it.
• A script file is executed by typing the name of the file (without the .m

A
D
extension) on the command line.

AD
• It is equivalent to typing all the commands stored in the script file,
one by one , at the MATLAB prompt.

U
• Naturally, script files work on global variables, that is, variables
VT
currently present in the workspace.
• Results obtained from executing script files are left in the workspace.
Example of script file

A
D
AD
U
VT
MATLAB Code

A
D
AD
U
VT
Function Files
• A function file is also an M-file, like a script file, except that the
variables in a function file are all local.

A
• Function files are like programs or subroutines in Fortran, procedures

D
AD
in Pascal, and functions in C.
• Once you get to know MATLAB well, this is where you are likely to

U
spend most of your time--writing and refining your own function files.

VT
• A function file begins with a function definition line, which has a well-
defined list of inputs and outputs. Without this line, the file becomes

A
a script file. The syntax of the function definition line is as follows:

D
AD
• function[output variables]= function name(input variables);
• where the function_name must be the same as the file name

U
(without the .m extension) in which the function is written. For

VT
example, if the name of the function is projecti le it must be written
and saved in a file with the name projectile.m
VT
U
AD
D
A
VT
U
AD
D
A
Example of function file

A
D
AD
U
VT
MATLAB Code

A
D
AD
U
VT
Language Specific Features
• Use of comments to create on-line help
• Continuation

A
D
• Global variables

AD
• Loops, branches, and control-flow

U
• Interactive input

VT
Use of comments to create on-line help
• The comment lines at the beginning (before any executable
statement) of a script or a function file are used by MATLAB as the on-
line help on that file. This automatically creates the on-line help for
user-written functions.

A
• Continuation:

D
AD
• An ellipsis (three consecutive periods) . at the end of a line denotes
continuation

U
• Examples: A = [1 3 3 3; 5 10 -2 -20 ; 3 5 …
• VT
10 2; 1 0 0 9] ;
• Global Variables:
• It is possible to declare a set of variables to be globally access.ible to

A
D
all or some functions without passing the variables in the input list.

AD
This is done with the global command.
• Loops, Branches and Control flow:

U
• For loop:
VT
• While loop:

A
D
AD
U
VT
• If-elseif-else statement:

A
D
AD
U
VT
• Switch-case-otherwise:

A
D
AD
U
VT
• Interactive input:
• Input:

A
D
• The command input ('string ' ), used in the previous example, displays

AD
the text in string on the screen and waits for the user to give
keyboard input.

U
• Keyboard:
VT
• The command keyboard inside a script or a function file returns
control to the keyboard at the point where the command occurs. The
execution of the function or the script is not terminated.
• Menu:
• The command menu ( ‘Menu Name’, ‘option1’, ‘option2’, ••• ) creates

A
D
an on-screen menu with the MenuName and lists the options in the

AD
menu. The user can select any of the options using the mouse or the
keyboard, depending on the computer

U
• Pause:

VT
• The command pause temporarily halts the current process. It can be
used with or without an optional argument

You might also like