Introduction To MATLAB
Introduction To MATLAB
WITH MATLAB
MATLAB Programs: In MATLAB, programs may be written and
saved in files with a suffix .m called M-files. There are two types of
M-file programs: functions and scripts.
Function Programs
Begin by clicking on the new document icon in the top left of the
MATLAB window (it looks like an empty sheet of paper). In the
document window type the following:
Save this file as: myfunc.m in your working directory. This file can
now be used in the command window just like any predefined
MATLAB function; in the command window enter:
Look back at the program. All function programs are like this one, the
essential elements are:
• Begin with the word function.
• There is an input and an output.
• The output, name of the function and the input must appear in the
first line.
• The body of the program must assign a value to the output
variable(s).
• The program cannot access variables in the current workspace
unless they are input.
workspace.
Functions can have multiple inputs, which are separated by commas.
For example:
Many people use script programs for routine calculations that would
require typing more than one command in the command window.
They do this because correcting mistakes is easier in a program than
in the command window.
Program Comments