MATLAB program are stored as script file(.m files). This type of file contains MATLAB commands, so running it is equivalent to typing all the commands—one at a time—at the Command window prompt. You can run the file by typing its name at the Command window.
2. Introduction
You can perform operations in MATLAB in two
ways:
1. In the interactive mode, in which all commands are
entered directly in the Command window
2. By running a MATLAB program stored in script file. This
type of file contains MATLAB commands, so running it is
equivalent to typing all the commands—one at a time—at
the Command window prompt. You can run the file by
typing its name at the Command window prompt.
4. Programming in MATLAB
There are two types of MATLAB programs
Al-Amer 2006
4
% script file
P=[1 3 2]
roots(P)
function [y]=fun(x)
y=x^2+3*x^2+2
script files function files
Script files
List of MATLAB statements
Variables are global
Run it by typing the file
name
Function files
Starts with function
List of MATLAB statements
Variables are local
5. Script
.m files
Both functions and scripts are stored in .m files
Type up a bunch of commands and save as filename.m
Type filename in command window to run
Example: first_program.m
The name of a script file must begin with a letter, and may include digits
and the underscore character, up to 63 characters.
Do not give a script file the same name as a variable.
Do not give a script file the same name as a MATLAB command or
function.
5
6. Function
Functions are more complex than scripts
Functions have their own local variables
Functions return output as specified, and can accept
input as specified
6
function [FC]=factorial(N)
FC=1;
for i=1:N
FC=FC*i;
end
6
output Function name
input
First statement
must start with
‘function’
7. Programming Style
Comments section
a. The name of the program and any key words in the first line.
b. The date created, and the creators' names in the second line.
c. The definitions of the variable names for every input and output
variable.
d. The name of every user-defined function called by the program.
Input section
Include input data and/or the input functions and comments for
documentation.
Calculation section
Output section
This section might contain functions for displaying the output on
the screen.
8. Example of a Script File
Problem: Plot a curve of Ohms law verification for a Resistor.
% Program ohms_law.m Plots curve of current versus
voltage.
% Created on March 1, 2009 by W. Palm
% i is current(in ampere)
% v is voltage (in volt)
% Input section:
i = input(’Enter the current value:’);
v = input(’Enter the voltage value:’);
% Calculation section:
R = v/i; % Compute the resistance.
% Output section:
fprintf('The resistance is %d',R)
9. Commenting
Comment your code!
Any line starting with % is a comment
Comments can be added to the end of existing lines
by adding a %
Note that anything after % will be ignored
>>% This is a comment.
>>x = 2+3 % So is this.
In editor screen comments are green
Any comments written at the beginning of an m-file
will be displayed by the command help filename
9
10. Commenting - An Example
function [FC]=factorial(N)
% [FC]=factorial(N)
% program to calculate the factorial of a number
% input N : an integer
% if N is not an integer the program obtains the
% factorial of the integer part of N
% output FC : the factorial of N
%
FC=1; % initial value of FC
for i=1:N
FC=FC*i; % n! =(n-1)!*n
end
10
These
comments
will be
displayed
when
‘help factorial’
is typed
Comments are used to explain
MATLAB statements