Matlab Programming Tips Part 1

10
1 Matlab Programming Tips Part 1– Scripts and Functions by Shameer Koya

Transcript of Matlab Programming Tips Part 1

Page 1: Matlab Programming Tips Part 1

11

Matlab Programming TipsPart 1– Scripts and Functions

by

Shameer Koya

Page 2: Matlab Programming Tips Part 1

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.

Page 3: Matlab Programming Tips Part 1

The MATLAB Editor/Debugger Window

Page 4: Matlab Programming Tips Part 1

Al-Amer 2006

Programming in MATLAB

There are two types of MATLAB programs4

% script file P=[1 3 2] roots(P)

function [y]=fun(x) y=x^2+3*x^2+2

script files function files

Script filesList of MATLAB statementsVariables are globalRun it by typing the file

name

Function files Starts with function List of MATLAB statements Variables are local

Page 5: Matlab Programming Tips Part 1

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

Page 6: Matlab Programming Tips Part 1

Function

Functions are more complex than scriptsFunctions have their own local variablesFunctions 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’

Page 7: Matlab Programming Tips Part 1

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.

Page 8: Matlab Programming Tips Part 1

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)

Page 9: Matlab Programming Tips Part 1

Commenting

Comment your code!Any line starting with % is a commentComments 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 greenAny comments written at the beginning of an m-file

will be displayed by the command help filename

9

Page 10: Matlab Programming Tips Part 1

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