SlideShare a Scribd company logo
11
Matlab Programming Tips
Part 1– Scripts and Functions
by
Shameer Koya
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.
The MATLAB Editor/Debugger Window
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
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
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’
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.
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)
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
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
Ad

Recommended

MATLAB programming tips 2 - Input and Output Commands
MATLAB programming tips 2 - Input and Output Commands
Shameer Ahmed Koya
 
Introduction to Matlab Scripts
Introduction to Matlab Scripts
Shameer Ahmed Koya
 
User Defined Functions in MATLAB part 2
User Defined Functions in MATLAB part 2
Shameer Ahmed Koya
 
MATLAB Scripts - Examples
MATLAB Scripts - Examples
Shameer Ahmed Koya
 
Matlab m files and scripts
Matlab m files and scripts
Ameen San
 
An Introduction to MATLAB for beginners
An Introduction to MATLAB for beginners
Murshida ck
 
Programming Environment in Matlab
Programming Environment in Matlab
DataminingTools Inc
 
MATLAB Programming
MATLAB Programming
محمدعبد الحى
 
User defined Functions in MATLAB Part 1
User defined Functions in MATLAB Part 1
Shameer Ahmed Koya
 
Basics of programming in matlab
Basics of programming in matlab
AKANKSHA GUPTA
 
Matlab programming project
Matlab programming project
Assignmentpedia
 
Matlab introduction
Matlab introduction
Ameen San
 
Matlab 1(operations on_matrix)
Matlab 1(operations on_matrix)
harman kaur
 
User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4
Shameer Ahmed Koya
 
Working with functions in matlab
Working with functions in matlab
harman kaur
 
Anonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLAB
Shameer Ahmed Koya
 
Brief Introduction to Matlab
Brief Introduction to Matlab
Tariq kanher
 
MATLAB BASICS
MATLAB BASICS
butest
 
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
reddyprasad reddyvari
 
C standard library functions
C standard library functions
Vaishnavee Sharma
 
Java 8
Java 8
Sheeban Singaram
 
Introduction to MATLAB
Introduction to MATLAB
Ravikiran A
 
Matlab commands
Matlab commands
Tarun Gehlot
 
C programming session5
C programming session5
Keroles karam khalil
 
Matlab practical and lab session
Matlab practical and lab session
Dr. Krishna Mohbey
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
amanabr
 
C Programming Unit-2
C Programming Unit-2
Vikram Nandini
 
Matlab Introduction
Matlab Introduction
Daniel Moore
 
User defined functions in matlab
User defined functions in matlab
Infinity Tech Solutions
 
Chap2programing.pptxdxnDSnfkezjnfqjdsckjqds
Chap2programing.pptxdxnDSnfkezjnfqjdsckjqds
yassinebrh31
 

More Related Content

What's hot (20)

User defined Functions in MATLAB Part 1
User defined Functions in MATLAB Part 1
Shameer Ahmed Koya
 
Basics of programming in matlab
Basics of programming in matlab
AKANKSHA GUPTA
 
Matlab programming project
Matlab programming project
Assignmentpedia
 
Matlab introduction
Matlab introduction
Ameen San
 
Matlab 1(operations on_matrix)
Matlab 1(operations on_matrix)
harman kaur
 
User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4
Shameer Ahmed Koya
 
Working with functions in matlab
Working with functions in matlab
harman kaur
 
Anonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLAB
Shameer Ahmed Koya
 
Brief Introduction to Matlab
Brief Introduction to Matlab
Tariq kanher
 
MATLAB BASICS
MATLAB BASICS
butest
 
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
reddyprasad reddyvari
 
C standard library functions
C standard library functions
Vaishnavee Sharma
 
Java 8
Java 8
Sheeban Singaram
 
Introduction to MATLAB
Introduction to MATLAB
Ravikiran A
 
Matlab commands
Matlab commands
Tarun Gehlot
 
C programming session5
C programming session5
Keroles karam khalil
 
Matlab practical and lab session
Matlab practical and lab session
Dr. Krishna Mohbey
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
amanabr
 
C Programming Unit-2
C Programming Unit-2
Vikram Nandini
 
Matlab Introduction
Matlab Introduction
Daniel Moore
 
User defined Functions in MATLAB Part 1
User defined Functions in MATLAB Part 1
Shameer Ahmed Koya
 
Basics of programming in matlab
Basics of programming in matlab
AKANKSHA GUPTA
 
Matlab programming project
Matlab programming project
Assignmentpedia
 
Matlab introduction
Matlab introduction
Ameen San
 
Matlab 1(operations on_matrix)
Matlab 1(operations on_matrix)
harman kaur
 
User Defined Functions in MATLAB Part-4
User Defined Functions in MATLAB Part-4
Shameer Ahmed Koya
 
Working with functions in matlab
Working with functions in matlab
harman kaur
 
Anonymous and Inline Functions in MATLAB
Anonymous and Inline Functions in MATLAB
Shameer Ahmed Koya
 
Brief Introduction to Matlab
Brief Introduction to Matlab
Tariq kanher
 
MATLAB BASICS
MATLAB BASICS
butest
 
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
MATLAB/SIMULINK for Engineering Applications day 2:Introduction to simulink
reddyprasad reddyvari
 
C standard library functions
C standard library functions
Vaishnavee Sharma
 
Introduction to MATLAB
Introduction to MATLAB
Ravikiran A
 
Matlab practical and lab session
Matlab practical and lab session
Dr. Krishna Mohbey
 
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
amanabr
 
Matlab Introduction
Matlab Introduction
Daniel Moore
 

Similar to Matlab Programming Tips Part 1 (20)

User defined functions in matlab
User defined functions in matlab
Infinity Tech Solutions
 
Chap2programing.pptxdxnDSnfkezjnfqjdsckjqds
Chap2programing.pptxdxnDSnfkezjnfqjdsckjqds
yassinebrh31
 
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
rajkumar490591
 
Palm m3 chapter1b
Palm m3 chapter1b
Juan Pablo Fuentes Encinas
 
Chapter 1.ppt
Chapter 1.ppt
Indra Hermawan
 
1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx
MOHAMMAD SAYDUL ALAM
 
Vb (1)
Vb (1)
Rajeev Sharan
 
Algorithm and c language
Algorithm and c language
kamalbeydoun
 
Chapter3
Chapter3
Kamran
 
matlabchapter1.ppt
matlabchapter1.ppt
PariaMotahari1
 
C notes.pdf
C notes.pdf
Durga Padma
 
Complete C programming Language Course
Complete C programming Language Course
Vivek Singh Chandel
 
Basic structure of c programming
Basic structure of c programming
TejaswiB4
 
Basic structure of c programming
Basic structure of c programming
TejaswiB4
 
Book management system
Book management system
SHARDA SHARAN
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
Sivakumar R D .
 
C programming course material
C programming course material
Ranjitha Murthy
 
Matlab-3.pptx
Matlab-3.pptx
aboma2hawi
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
indrasir
 
Introduction to matlab
Introduction to matlab
Tarun Gehlot
 
Chap2programing.pptxdxnDSnfkezjnfqjdsckjqds
Chap2programing.pptxdxnDSnfkezjnfqjdsckjqds
yassinebrh31
 
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
rajkumar490591
 
Algorithm and c language
Algorithm and c language
kamalbeydoun
 
Chapter3
Chapter3
Kamran
 
Complete C programming Language Course
Complete C programming Language Course
Vivek Singh Chandel
 
Basic structure of c programming
Basic structure of c programming
TejaswiB4
 
Basic structure of c programming
Basic structure of c programming
TejaswiB4
 
Book management system
Book management system
SHARDA SHARAN
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
Sivakumar R D .
 
C programming course material
C programming course material
Ranjitha Murthy
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
indrasir
 
Introduction to matlab
Introduction to matlab
Tarun Gehlot
 
Ad

More from Shameer Ahmed Koya (6)

MATLAB Programming - Loop Control Part 2
MATLAB Programming - Loop Control Part 2
Shameer Ahmed Koya
 
Matlab Script - Loop Control
Matlab Script - Loop Control
Shameer Ahmed Koya
 
Conditional Control in MATLAB Scripts
Conditional Control in MATLAB Scripts
Shameer Ahmed Koya
 
Polynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLAB
Shameer Ahmed Koya
 
MATLAB - Aplication of Arrays and Matrices in Electrical Systems
MATLAB - Aplication of Arrays and Matrices in Electrical Systems
Shameer Ahmed Koya
 
MATLAB - Arrays and Matrices
MATLAB - Arrays and Matrices
Shameer Ahmed Koya
 
MATLAB Programming - Loop Control Part 2
MATLAB Programming - Loop Control Part 2
Shameer Ahmed Koya
 
Conditional Control in MATLAB Scripts
Conditional Control in MATLAB Scripts
Shameer Ahmed Koya
 
Polynomials and Curve Fitting in MATLAB
Polynomials and Curve Fitting in MATLAB
Shameer Ahmed Koya
 
MATLAB - Aplication of Arrays and Matrices in Electrical Systems
MATLAB - Aplication of Arrays and Matrices in Electrical Systems
Shameer Ahmed Koya
 
Ad

Recently uploaded (20)

GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
List View Components in Odoo 18 - Odoo Slides
List View Components in Odoo 18 - Odoo Slides
Celine George
 
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
SHERAZ AHMAD LONE
 
Peer Teaching Observations During School Internship
Peer Teaching Observations During School Internship
AjayaMohanty7
 
A Visual Introduction to the Prophet Jeremiah
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
parmarjuli1412
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
LDMMIA Shop & Student News Summer Solstice 25
LDMMIA Shop & Student News Summer Solstice 25
LDM & Mia eStudios
 
Intellectual Property Right (Jurisprudence).pptx
Intellectual Property Right (Jurisprudence).pptx
Vishal Chanalia
 
Aprendendo Arquitetura Framework Salesforce - Dia 02
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
Photo chemistry Power Point Presentation
Photo chemistry Power Point Presentation
mprpgcwa2024
 
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
GREAT QUIZ EXCHANGE 2025 - GENERAL QUIZ.pptx
Ronisha Das
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
Filipino 9 Maikling Kwento Ang Ama Panitikang Asiyano
sumadsadjelly121997
 
List View Components in Odoo 18 - Odoo Slides
List View Components in Odoo 18 - Odoo Slides
Celine George
 
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
SHERAZ AHMAD LONE
 
Peer Teaching Observations During School Internship
Peer Teaching Observations During School Internship
AjayaMohanty7
 
A Visual Introduction to the Prophet Jeremiah
A Visual Introduction to the Prophet Jeremiah
Steve Thomason
 
Pests of Maize: An comprehensive overview.pptx
Pests of Maize: An comprehensive overview.pptx
Arshad Shaikh
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
SCHIZOPHRENIA OTHER PSYCHOTIC DISORDER LIKE Persistent delusion/Capgras syndr...
parmarjuli1412
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
LDMMIA Shop & Student News Summer Solstice 25
LDMMIA Shop & Student News Summer Solstice 25
LDM & Mia eStudios
 
Intellectual Property Right (Jurisprudence).pptx
Intellectual Property Right (Jurisprudence).pptx
Vishal Chanalia
 
Aprendendo Arquitetura Framework Salesforce - Dia 02
Aprendendo Arquitetura Framework Salesforce - Dia 02
Mauricio Alexandre Silva
 
K12 Tableau User Group virtual event June 18, 2025
K12 Tableau User Group virtual event June 18, 2025
dogden2
 
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
2025 June Year 9 Presentation: Subject selection.pptx
2025 June Year 9 Presentation: Subject selection.pptx
mansk2
 
Photo chemistry Power Point Presentation
Photo chemistry Power Point Presentation
mprpgcwa2024
 
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
LAZY SUNDAY QUIZ "A GENERAL QUIZ" JUNE 2025 SMC QUIZ CLUB, SILCHAR MEDICAL CO...
Ultimatewinner0342
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 

Matlab Programming Tips Part 1

  • 1. 11 Matlab Programming Tips Part 1– Scripts and Functions by Shameer Koya
  • 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